未被“识别”识别;使用 validates_each 时出现命令错误
下面是我对图像内容类型的验证,效果很好。
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']
但是
我还需要验证尺寸,我的代码是
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错误无法被'identify'命令识别错误
你能解释一下吗?
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用“validate”方法而不是“validates_each”怎么样
http://paulsturgess.co .uk/articles/33-how-to-write-custom-validation-in-ruby-on-rails
How about using "validate" method instead of "validates_each"
http://paulsturgess.co.uk/articles/33-how-to-write-custom-validation-in-ruby-on-rails
您的计算机上似乎没有安装 ImageMagick。如果您这样做,请输入
路径并将其作为值添加到
environment.rb
中的以下回形针选项让知道它是怎么回事..
Looks like you do not have ImageMagick installed on your machine. If you do, type
and add the path as value to the following paperclip option in
environment.rb
Let know how it goes..
最后,在您的帮助下完成了。
我希望 validates_each 在其他验证之前执行或覆盖其他验证。没有把握 :(
Finally, did with the help of yours input.
I hope the validates_each executed before or override the other validations. Not sure :(