Ruby-on-rails 验证附件内容类型回形针

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3181845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:51:34  来源:igfitidea点击:

Validate Attachment Content Type Paperclip

ruby-on-railspaperclip

提问by Kevin Sylvestre

Is it possible to enforce a 'content type' validation in paperclipwithout 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.

是否可以仅在包含附件的情况下进行验证。

回答by Jesse Wolgamott

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

Then, if I:

那么,如果我:

Image.new.valid?
#this is true

You might be doing other paperclip validations, though. Can you post a simple example?

不过,您可能正在进行其他回形针验证。你能贴一个简单的例子吗?

回答by mcls

Working example

工作示例

In the following model only image/png, image/gif and image/jpegare valid content types for the image attachment.

在以下模型中,只有image/png、image/gif 和 image/jpeg是图像附件的有效内容类型。

class Photo
  has_attached_file :image
  validates_attachment_content_type :image, 
                                    :content_type => /^image\/(png|gif|jpeg)/
end

Specs

眼镜

describe Photo do
  it { should validate_attachment_content_type(:image).  
              allowing('image/png', 'image/gif', 'image/jpeg').      
              rejecting('text/plain', 'text/xml', 'image/abc', 'some_image/png') }
end

More info

更多信息

You could also take a look at the AttachmentContentTypeValidatorclass with is responsible for doing the validation.

您还可以查看负责进行验证的AttachmentContentTypeValidator类。

Or take a look at its testswhich contain more examples.

或者看看它包含更多示例的测试

回答by Tim Snowhite

validates_content_type accepts :if => Proc.new{|r| !r.content_type.blank?}in it's options hash, perhaps that would solve your problem.

validates_content_type:if => Proc.new{|r| !r.content_type.blank?}在它的选项哈希中接受,也许这会解决你的问题。

http://rdoc.info/github/thoughtbot/paperclip#

http://rdoc.info/github/thoughtbot/paperclip#

回答by Bernard Banta

This worked for me;

这对我有用;

validates_attachment :image1, :presence => true,
                         :content_type => { :content_type => "image/jpg" },
                         :size => { :in => 0..10.kilobytes }