ActiveStorage File Attachment Validation

前端 未结 7 540
暖寄归人
暖寄归人 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:31

    ActiveStorage doesn't support validations right now. According to https://github.com/rails/rails/issues/31656.


    Update:

    Rails 6 will support ActiveStorage validations.

    https://github.com/rails/rails/commit/e8682c5bf051517b0b265e446aa1a7eccfd47bf7

    Uploaded files assigned to a record are persisted to storage when the record
    is saved instead of immediately.
    In Rails 5.2, the following causes an uploaded file in `params[:avatar]` to
    be stored:
    ```ruby
    @user.avatar = params[:avatar]
    ```
    In Rails 6, the uploaded file is stored when `@user` is successfully saved.
    

提交回复
热议问题