使用validates_each时,'identify'命令错误无法识别

时间:2011-08-12 08:12:24

标签: ruby-on-rails paperclip ruby-on-rails-2 paperclip-validation

以下是我对图片内容类型的验证,效果很好。

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

我还需要验证尺寸,我的代码是

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

它提供了imagemagick错误Not recognized by the 'identify' command error

你能对此有所了解吗?

感谢。

3 个答案:

答案 0 :(得分:1)

如何使用“validate”方法代替“validates_each” http://paulsturgess.co.uk/articles/33-how-to-write-custom-validation-in-ruby-on-rails

答案 1 :(得分:0)

您的计算机上似乎没有安装ImageMagick。如果您这样做,请键入

  

识别

并将路径作为值添加到environment.rb

中的以下回形针选项
  

Paperclip.options [:command_path] =“/ usr / local / bin /”#assuming this folder

让我们知道它是怎么回事......

答案 2 :(得分:0)

最后,在你的帮助下做了输入。

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

我希望之前执行validates_each或覆盖其他验证。不确定:(