验证附件内容类型回形针 [英] Validate Attachment Content Type Paperclip
本文介绍了验证附件内容类型回形针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以在 paperclip 中强制执行内容类型"验证而不强制执行存在"验证(即允许空格)?我目前有:
Is it possible to enforce a 'content type' validation in paperclip without enforcing a 'presence' validation (i.e. allow blanks)? I currently have:
class Person < ActiveRecord::Base
has_attached_file :picture
validates_attachment_content_type :picture, :content_type => ['image/jpeg', 'image/jpg', 'image/png']
end
但是,如果不存在附件,这将失败.例如:
However, this fails if no attachment is present. For example:
>> @person = Person.new
>> @person.save
>> @person.errors.first
=> ["picture_content_type", "is not one of image/jpeg, image/jpg, image/png"]
是否可以仅在包含附件的情况下进行验证.
Is it possible to do the validation only if an attachment is included.
推荐答案
我不确定该方法是否是您失败的原因;这是我的简单课程
I'm not sure that method is the cause of your failure; Here's my simple class
class Image < ActiveRecord::Base
has_attached_file :photo, {
:styles => { :large => "700x400#", :medium=>"490x368#", :thumbnail=>"75x75#" },
:default_url => "/images/thumbnail/blank-recipe.png"}
validates_attachment_content_type :photo, :content_type => /image/
end
那么,如果我:
Image.new.valid?
#this is true
不过,您可能正在进行其他回形针验证.你能贴一个简单的例子吗?
You might be doing other paperclip validations, though. Can you post a simple example?
这篇关于验证附件内容类型回形针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文