1

我们目前正在这样做以验证操作文本的上传,并且工作正常。有没有办法将此验证移至服务器,即在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")
  }
})
4

1 回答 1

0

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

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

has_one_attached: file      
validates :file, attached: true, size: { less_than: 100.megabytes , message: 'too big' }
于 2020-10-12T10:09:29.903 回答