有什么办法可以防止Paperclip上传验证两次出现验证消息?
这是我的模型:
has_attached_file :photo, :styles => { :thumb => "215x165" }, :default_url => "/images/:style/missing.png"
validates_attachment :photo, :presence => true,
:content_type => { :content_type => "image/jpg" },
:size => { :in => 0..0.5.megabytes }
这是我的看法:
<% if @product.errors.any? %>
<p>The following errors were found:</p>
<ul>
<% @product.errors.full_messages.each do |message| %>
<li>- <%= message %></li>
<% end %>
</ul>
<% end %>
如果我上传了无效的文件,则会收到以下错误消息:
有什么方法可以使其中之一出现吗?我尝试将消息:添加到模型。但这又出现了两次!
谢谢!
如果检查@ model.errors哈希,可以看到它为:photo属性返回一个数组,并为每个回形针验证器返回一条消息。
{:photo_content_type=>["is invalid"],
:photo=>["is invalid", "must be less than 1048576 Bytes"],
:photo_file_size=>["must be less than 1048576 Bytes"] }
您需要使用一些Ruby过滤掉其中的很多。有很多方法可以解决此问题(有关一些想法,请参见此处),但快速的解决方法是删除:photo数组,并仅使用回形针生成的属性中的消息。
@model.errors.delete(:photo)
这应该给你一个@model.errors.full_messages
这样的:
["Photo content type is invalid", "Photo file size must be less than 1048576 Bytes"]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句