Ruby-on-rails Paperclip::Errors::MissingRequiredValidatorError with Rails 4
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21897725/
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
Paperclip::Errors::MissingRequiredValidatorError with Rails 4
提问by nadia
I'm getting this error when I try to upload using paperclip with my rails blogging app. Not sure what it is referring to when it says "MissingRequiredValidatorError" I thought that by updating post_params and giving it :image it would be fine, as both create and update use post_params
当我尝试使用带有 Rails 博客应用程序的回形针上传时,出现此错误。不确定当它说“MissingRequiredValidatorError”时它指的是什么我认为通过更新 post_params 并给它 :image 就可以了,因为创建和更新都使用 post_params
Paperclip::Errors::MissingRequiredValidatorError in PostsController#create
Paperclip::Errors::MissingRequiredValidatorError
Extracted source (around line #30):
def create
@post = Post.new(post_params)
This is my posts_controller.rb
这是我的posts_controller.rb
def update
@post = Post.find(params[:id])
if @post.update(post_params)
redirect_to action: :show, id: @post.id
else
render 'edit'
end
end
def new
@post = Post.new
end
def create
@post = Post.new(post_params)
if @post.save
redirect_to action: :show, id: @post.id
else
render 'new'
end
end
#...
private
def post_params
params.require(:post).permit(:title, :text, :image)
end
and this is my posts helper
这是我的帖子助手
module PostsHelper
def post_params
params.require(:post).permit(:title, :body, :tag_list, :image)
end
end
Please let me know if I can supplement extra material to help you help me.
请让我知道我是否可以补充额外的材料来帮助你帮助我。
回答by Kirti Thorat
Starting with Paperclip version 4.0, all attachments are required to include a content_type validation, a file_name validation, or to explicitlystate that they're not going to have either.
以 开头Paperclip version 4.0,所有附件都需要包含content_type 验证、file_name 验证,或者明确声明它们不会有。
Paperclip raises Paperclip::Errors::MissingRequiredValidatorErrorerror if you do not do any of this.
Paperclip::Errors::MissingRequiredValidatorError如果您不执行任何操作,Paperclip 会引发错误。
In your case, you can add any of the following line to your Postmodel, afterspecifying has_attached_file :image
在您的情况下,您可以在指定后将以下任何行添加到您的Post模型中has_attached_file :image
Option 1: Validate content type
选项 1:验证内容类型
validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
-OR- another way
- 或 - 另一种方式
validates_attachment :image, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png", "image/gif"] }
-OR- yet another way
- 或 - 另一种方式
is to use regexfor validating content type.
是使用正则表达式来验证内容类型。
For example: To validate all image formats, regex expression can be specified as shown in
例如:要验证所有图像格式,可以指定正则表达式,如下所示
Option 2: Validate filename
选项 2:验证文件名
validates_attachment_file_name :image, :matches => [/png\Z/, /jpe?g\Z/, /gif\Z/]
Option 3: Do not validate
选项 3:不验证
If for some crazyreason (can be validbut I cannot think of one right now), you do not wish to add any content_typevalidation and allow people to spoof Content-Types and receive data you weren't expecting onto your server then add the following:
如果出于某种疯狂的原因(可能是有效的,但我现在想不出一个),您不希望添加任何content_type验证并允许人们欺骗内容类型并在您的服务器上接收您不期望的数据,然后添加以下内容:
do_not_validate_attachment_file_type :image
Note:
笔记:
Specify the MIME types as per your requirement within content_type/ matchesoptions above.I have just given a few image MIME types for you to start with.
在上面的content_type/matches选项中根据您的要求指定 MIME 类型。我刚刚给出了一些图像 MIME 类型供您开始。
Reference:
参考:
Refer to Paperclip: Security Validations, if you still need to verify. :)
You might also have to deal with the spoofing validation explained here https://stackoverflow.com/a/23846121
您可能还需要处理此处解释的欺骗验证https://stackoverflow.com/a/23846121
回答by Lucas Caton
Just put in your model:
只需放入您的模型:
validates_attachment :image, content_type: { content_type: /\Aimage\/.*\Z/ }
回答by Arvind singh
Need to add validates_attachment_content_typein Model
需要在Model中添加validates_attachment_content_type
Rails 3
导轨 3
class User < ActiveRecord::Base
attr_accessible :avatar
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
end
Rails 4
导轨 4
class User < ActiveRecord::Base
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
end
回答by shriyog
Make sure your post model looks like this...
确保你的帖子模型看起来像这样......
class Post < ActiveRecord::Base
has_attached_file :photo
validates_attachment_content_type :photo, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
end
回答by Ric
I couldn't get any of these solutions to work either. I tried Paperclip 3.1, but couldn't then my app kept telling me my image file extensions weren't approved, even though they were jpg.
我也无法使这些解决方案中的任何一个起作用。我尝试了 Paperclip 3.1,但是我的应用程序一直告诉我我的图像文件扩展名未被批准,即使它们是 jpg。
I finally found success with version 3.5.1.
我终于在 3.5.1 版上取得了成功。

