转换后的Rails Paperclip content_type验证错误&显示错误的错误

时间:2011-03-15 16:54:08

标签: ruby-on-rails paperclip

Paperclip插件验证内容类型时遇到问题:

image.rb(相关摘录)

  has_attached_file :photo,
    :styles => {:xlarge => "640x512>", :large => "350x280>", :medium => "180x144^",
                :thumb => "100x80^", :original => "1280x1280>" },
    :convert_options => {:xlarge => "-strip", :large => "-strip",
                :medium => "-strip -gravity center -extent 180x144 +repage",
                :thumb => "-strip -gravity center -extent 100x80 +repage"},
    :default_style => :medium,
    :url => "/system/:attachment/:id/:basename_:id_:style.:extension"


  validates_attachment_presence :photo
  validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/png', 'image/gif', 'image/pjpeg', 'image/x-png', 'image/jpeg2000'], :message => 'Uploaded file is not an image'

我正在使用嵌套表单,图像模型是属性模型的子模型。 在测试我的表单的验证期间,我遇到了一些问题。我正在通过上传PDF文件来测试验证,其中模型仅接受图像。

在我的表单视图中输出

:<%= @ property.errors.inspect%>

#[#, @message="Uploaded file is not an image">], 
"images.photo"=>[#, @message="C:/Users/Michael/AppData/Local/Temp/stream,4272,1.pdf is not recognized by the 'identify' command.">]}>, @base=#>

在文件上传输入框的验证错误视图中输出:

C:/Users/Michael/AppData/Local/Temp/stream,4272,1.pdf is not recognized by the 'identify' command.
C:/Users/Michael/AppData/Local/Temp/stream,4272,1.pdf is not recognized by the 'identify' command.
C:/Users/Michael/AppData/Local/Temp/stream,4272,1.pdf is not recognized by the 'identify' command.
C:/Users/Michael/AppData/Local/Temp/stream,4272,1.pdf is not recognized by the 'identify' command.
C:/Users/Michael/AppData/Local/Temp/stream,4272,1.pdf is not recognized by the 'identify' command.

2个问题:

第1期

输入字段显示相同错误的5倍,这是因为我在模型中设置了5种样式。问题是,为什么回形针试图识别(并且可能转换pdf),当你希望验证首先运行并在尝试识别和转换之前返回已经错误的时候? Paperclip在运行验证之前是否转换为所有样式?如果是,是否有办法切换处理顺序,首先验证,然后是样式处理?

第2期

如何在文件上传输入框旁边显示验证错误消息('上载文件不是图像'),而不是现在显示的错误(5 x识别命令的输出),后者是对网站用户没用。

谢谢!

1 个答案:

答案 0 :(得分:1)

我用过

has_attached_file:photo,:whiny =>假

这似乎帮助了错误消息。对于错误消息,我正在使用

OBJECT.errors [:photo_content_type] OBJECT.errors [:photo_file_size]