如何将动作文本附件的验证从javascript转移到模型

问题描述

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

models / course.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' }

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...