ActiveStorage File Attachment Validation

前端 未结 7 537
暖寄归人
暖寄归人 2021-01-31 10:01

Is there a way to validate attachments with ActiveStorage? For example, if I want to validate the content type or the file size?

Something like Paperclip\'s approach wou

7条回答
  •  生来不讨喜
    2021-01-31 10:19

    You can use awesome https://github.com/musaffa/file_validators gem

    class Profile < ActiveRecord::Base
      has_one_attached :avatar
      validates :avatar, file_size: { less_than_or_equal_to: 100.kilobytes },
        file_content_type: { allow: ['image/jpeg', 'image/png'] }
    end
    

    I'm using it with form object so I'm not 100% sure it is working directly with AR but it should...

提交回复
热议问题