Below is my validations for image content type which works fine.
validates_attachment_size :icon, :less_than => MAX_SIZE.megabytes, :message => "Max size is 1 mb" validates_attachment_content_type :icon, :content_type => ['image/jpg','image/jpeg', 'image/png', 'image/gif']
BUT
I need to validate the dimensions also and my code is
validates_each :icon do |record, attr, value| if record.icon_file_name dimensions = Paperclip::Geometry.from_file(value.queued_for_write[:original]) if(dimensions.width > 600 || dimensions.height > 400) record.errors.add(:file, " #{record.i开发者_开发知识库con_file_name} dimensions must be less than or equal to 600*400") end end end
AND
it gives imagemagick error Not recognized by the 'identify' command error
Can you put some light on this?
Thanks.
How about using "validate" method instead of "validates_each" http://paulsturgess.co.uk/articles/33-how-to-write-custom-validation-in-ruby-on-rails
Looks like you do not have ImageMagick installed on your machine. If you do, type
which identify
and add the path as value to the following paperclip option in environment.rb
Paperclip.options[:command_path] = "/usr/local/bin/" #assuming this folder
Let know how it goes..
Finally, did with the help of yours input.
validate :icon_dimensions
def icon_dimensions unless icon.to_file.nil? dimensions = Paperclip::Geometry.from_file(icon.to_file(:original)) if(dimensions.width > 72 || dimensions.height > 72) errors.add(:icon, " dimensions must be less than or equal to 72*72") end end end
I hope the validates_each executed before or override the other validations. Not sure :(
精彩评论