Paperclip::Errors::MissingRequiredValidatorError with Rails 4

只愿长相守 提交于 2019-11-27 10:05:22
Kirti Thorat

Starting with Paperclip version 4.0, all attachments are required to include a content_type validation, a file_name validation, or to explicitly state that they're not going to have either.

Paperclip raises Paperclip::Errors::MissingRequiredValidatorError error if you do not do any of this.

In your case, you can add any of the following line to your Post model, after specifying has_attached_file :image

Option 1: Validate content type

validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]

-OR- another way

validates_attachment :image, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png", "image/gif"] }

-OR- yet another way

is to use regex for validating content type.

For example: To validate all image formats, regex expression can be specified as shown in

@LucasCaton's answer

Option 2: Validate filename

validates_attachment_file_name :image, :matches => [/png\Z/, /jpe?g\Z/, /gif\Z/]

Option 3: Do not validate

If for some crazy reason (can be valid but I cannot think of one right now), you do not wish to add any content_type validation and allow people to spoof Content-Types and receive data you weren't expecting onto your server then add the following:

do_not_validate_attachment_file_type :image

Note:

Specify the MIME types as per your requirement within content_type/ matches options above. I have just given a few image MIME types for you to start with.

Reference:

Refer to Paperclip: Security Validations, if you still need to verify. :)

You might also have to deal with the spoofing validation explained here https://stackoverflow.com/a/23846121

Lucas Caton

Just put in your model:

validates_attachment :image, content_type: { content_type: /\Aimage\/.*\Z/ }

https://github.com/thoughtbot/paperclip

Need to add validates_attachment_content_type in Model

Rails 3

class User < ActiveRecord::Base
attr_accessible :avatar
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/ 
end

Rails 4

class User < ActiveRecord::Base
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
end

Make sure your post model looks like this...

class Post < ActiveRecord::Base
    has_attached_file :photo
    validates_attachment_content_type :photo, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
end

I couldn't get any of these solutions to work either. I tried Paperclip 3.1, but couldn't then my app kept telling me my image file extensions weren't approved, even though they were jpg.

I finally found success with version 3.5.1.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!