Search code examples
ruby-on-railspaperclipruby-on-rails-2paperclip-validation

Not recognized by the 'identify' command error while using validates_each


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.icon_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.


Solution

  • How about using "validate" method instead of "validates_each" http://paulsturgess.co.uk/articles/33-how-to-write-custom-validation-in-ruby-on-rails