Paperclip::Errors::MissingRequiredValidatorError with Rails 4
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
Paperclip::Errors::MissingRequiredValidatorError
Form the paperclip 4.0 onwards validating the attachment content type is mandatory. So please add the validation logic in your model to work.
class Document < ActiveRecord::Base
has_attached_file :item
validates_attachment_content_type :item, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif", "application/pdf"]
end
Rails : Paperclip MissingRequiredValidatorError
You have to have to include a content_type validation, something like this:
validates_attachment_content_type :attachment, :content_type => ["image/jpg", "image/jpeg"]
or you can replace your current validation with this:
validates_attachment_file_name :attachment, :matches => [/jpe?g\Z/]
Problems with paperclip on Rails 5
Link provided by hamdi in the first comment is an answer.
If you've got problems with Devise or with Paperclip like that, don't try to add "include Paperclip::Glue" or in case of Devise "extend Devise::Models". The only right way to fix this is to rollback ALL of your migrations, terminate console, start it again, migrate, terminate console one more time and thats it! Sounds stupid but it's working >< Pictures upload is working and paperclip generator is on the list.
For all of you who came from Rails 4:
2.5.0 :001 > Photo
=> Photo (call 'Photo.connection' to establish a connection)
is totally ok behavior for console in Rails 5. Before accessing model you've got to perform 'Photo.connection' from now on. If you don't like it you can always fix it by adding
console do
ActiveRecord::Base.connection
end
to your config/application.rb
Paperclip triggers validation error on destroy
Yes it is, use the :create
and/or :update
options with your custom validation.
You will do something like this:
validate :ratiocorrect, on: :create
By default, such validations will run every time you call valid? or save the object. But it is also possible to control when to run these custom validations by giving an :on option to the validate method, with either: :create or :update.
Check out Custom Methods for additional information.
Paperclip dimension validation error in Rails 4
Try this.
validates :image, :unless => "image.queued_for_write[:original].blank?", dimensions: { width: 800, height: 550 }
Paperclip type validation always giving error
I went into the same issue than you (even tried the other ways the thread suggests), and Rachel is right about the version. Paperclip 4 has some issue with spoofing. Changing it to 3.5 solved my problem.
Btw, if you still want to make it work, it seems to be the issue discussed here ( with a workaround given )
Rails paperclip not showing image
This error clearly show that your images are not getting saved because path and url not specified in your has_attached_file
block. It should be like :
has_attached_file :facepic,
:path => ":rails_root/public/system/:attachment/:id/:style/:filename",
:url => "/system/:attachment/:id/:style/:filename",
:styles => { :medium => "300x300>", :thumb => "100x100>" },
:default_url => "path to default image"
here default_url show image that you want if no image uploaded. For more detail you can go here http://rdoc.info/gems/paperclip/4.2.0/Paperclip/ClassMethods%3ahas_attached_file .
And for other error you can follow this link Paperclip::Errors::MissingRequiredValidatorError with Rails 4
Related Topics
Rails - Best-Practice: How to Create Dependent Has_One Relations
What's the 'Ruby Way' to Iterate Over Two Arrays at Once
Is the Server Running on Host "Localhost" (::1) and Accepting Tcp/Ip Connections on Port 5432
Differencebetween Raising Exceptions VS Throwing Exceptions in Ruby
Best Way to Add Comments in Erb
Best Ruby on Rails Social Networking Framework
What Is Java Interface Equivalent in Ruby
What Are Some Good Ruby-Based Web Crawlers
Is There a Better Way of Checking Nil or Length == 0 of a String in Ruby
Uninstalling All Gems Ruby 2.0.0
Ruby on Rails Console Is Hanging When Loading
Why Is Sum So Much Faster Than Inject(:+)
Rails Respond_To Format.Js API