如何将Action Text附件的验证从javascript转移到模型



我们目前这样做是为了验证上传的操作文本,并且运行正常。有没有办法将此验证转移到服务器,即在course.rb中而不是在javascript中?

模型/课程.rb

class Course < ApplicationRecord 
has_rich_text :description
has_one :description_text, class_name: 'ActionText::RichText', as: :record
end

javascript/packs/application.js

window.addEventListener("trix-file-accept", function(event) {
const acceptedTypes = ['image/jpeg', 'image/png', 'application/pdf']
if (!acceptedTypes.includes(event.file.type)) {
event.preventDefault()
alert("Attachment types supported are jpeg, png, and pdf")
}
const maxFileSize = 1024 * 1024 // 1MB
if (event.file.size > maxFileSize) {
event.preventDefault()
alert("Attachment size must be less than 1 MB")
}
})

有一个宝石https://github.com/igorkasyanchuk/active_storage_validations

你将能够在你的模型中写一个常规的验证:

has_one_attached: file      
validates :file, attached: true, size: { less_than: 100.megabytes , message: 'too big' }

相关内容

  • 没有找到相关文章

最新更新