未被“识别”识别;使用 validates_each 时出现命令错误

发布于 2024-11-29 00:09:41 字数 816 浏览 0 评论 0原文

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

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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

流殇 2024-12-06 00:09:41

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

识别

路径并将其作为值添加到 environment.rb 中的以下回形针选项

Paperclip.options[:command_path] = "/usr/local/bin/" #假设这个文件夹

让知道它是怎么回事..

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

燕归巢 2024-12-06 00:09:41

最后,在您的帮助下完成了。

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 在其他验证之前执行或覆盖其他验证。没有把握 :(

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 :(

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文