Paperclip::Errors::MissingRequiredValidatorError with Rails 4 [英] Paperclip::Errors::MissingRequiredValidatorError with Rails 4
问题描述
当我尝试使用带有 Rails 博客应用程序的回形针上传时遇到此错误.当它说MissingRequiredValidatorError"时不确定它指的是什么我认为通过更新 post_params 并给它 :image 就可以了,因为 create 和 update 都使用 post_params
Paperclip::Errors::MissingRequiredValidatorError in PostsController#create回形针::错误::MissingRequiredValidatorError提取的源代码(大约第 30 行):定义创建@post = Post.new(post_params)
这是我的posts_controller.rb
def 更新@post = Post.find(params[:id])如果@post.update(post_params)redirect_to 动作: :show, id: @post.id别的渲染编辑"结尾结尾定义新@post = Post.new结尾定义创建@post = Post.new(post_params)如果@post.saveredirect_to 动作: :show, id: @post.id别的呈现新"结尾结尾#...私人的def post_paramsparams.require(:post).permit(:title, :text, :image)结尾
这是我的帖子助手
模块 PostsHelperdef post_paramsparams.require(:post).permit(:title, :body, :tag_list, :image)结尾结尾
如果我可以补充额外的材料来帮助我,请告诉我.
从 Paperclip version 4.0
开始,所有附件都需要包含 content_type 验证,文件名验证,或者明确声明他们不会有.
Paperclip 会引发 Paperclip::Errors::MissingRequiredValidatorError
错误,如果您不这样做.
在您的情况下,您可以将以下任一行添加到您的 Post
模型中,after 指定 has_attached_file :image
选项 1:验证内容类型
validates_attachment_content_type :image, :content_type =>[图像/jpg"、图像/jpeg"、图像/png"、图像/gif"]
-或-另一种方式
validates_attachment :image, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png", "image/gif"] }
-或-另一种方式
是使用 regex 来验证内容类型.
例如:要验证所有图像格式,可以指定正则表达式,如
选项 2:验证文件名
validates_attachment_file_name :image, :matches =>[/png\Z/,/jpe?g\Z/,/gif\Z/]
选项 3:不验证
如果出于某种疯狂原因(可以有效但我现在想不出一个),您不想添加任何content_type
验证并允许人们欺骗内容类型并在您的服务器上接收您不期望的数据,然后添加以下内容:
do_not_validate_attachment_file_type :image
<小时>
注意:
在上面的 content_type
/matches
选项中根据您的要求指定 MIME 类型.我刚刚为您提供了一些图像 MIME 类型开始.
参考:
请参阅Paperclip:安全验证,如果您还需要验证.:)
您可能还需要处理此处解释的欺骗验证https://stackoverflow.com/a/23846121>
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
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
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.
Starting with Paperclip version 4.0
, all attachments are required to include a content_type validation, a file_name validation, or to explicitly state that they're not going to have either.
Paperclip raises Paperclip::Errors::MissingRequiredValidatorError
error if you do not do any of this.
In your case, you can add any of the following line to your Post
model, after specifying has_attached_file :image
Option 1: Validate content type
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 regex for validating content type.
For example: To validate all image formats, regex expression can be specified as shown in
Option 2: Validate filename
validates_attachment_file_name :image, :matches => [/png\Z/, /jpe?g\Z/, /gif\Z/]
Option 3: Do not validate
If for some crazy reason (can be valid but I cannot think of one right now), you do not wish to add any content_type
validation and allow people to spoof Content-Types and receive data you weren't expecting onto your server then add the following:
do_not_validate_attachment_file_type :image
Note:
Specify the MIME types as per your requirement within content_type
/ matches
options above. I have just given a few image MIME types for you to start with.
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
这篇关于Paperclip::Errors::MissingRequiredValidatorError with Rails 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!