nad*_*dia 224 ruby ruby-on-rails paperclip ruby-on-rails-4
当我尝试使用带有我的rails blogging app的paperclip上传时,我收到此错误.当它说"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)
Run Code Online (Sandbox Code Playgroud)
这是我的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
Run Code Online (Sandbox Code Playgroud)
这是我的帖子助手
module PostsHelper
def post_params
params.require(:post).permit(:title, :body, :tag_list, :image)
end
end
Run Code Online (Sandbox Code Playgroud)
如果我能补充额外的材料来帮助你,请告诉我.
Kir*_*rat 494
从一开始Paperclip version 4.0
,所有附件都需要包含content_type验证,file_name验证,或者明确声明它们不会有任何附件.
Paperclip::Errors::MissingRequiredValidatorError
如果您不执行任何操作,Paperclip会引发错误.
在您的情况下,您可以在指定后将任何以下行添加到Post
模型中has_attached_file :image
validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
Run Code Online (Sandbox Code Playgroud)
- 或 - 另一种方式
validates_attachment :image, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png", "image/gif"] }
Run Code Online (Sandbox Code Playgroud)
- 还是另一种方式
是使用正则表达式来验证内容类型.
例如:要验证所有图像格式,可以如下所示指定正则表达式
validates_attachment_file_name :image, :matches => [/png\Z/, /jpe?g\Z/, /gif\Z/]
Run Code Online (Sandbox Code Playgroud)
如果出于某些疯狂的原因(可能有效但我现在想不到一个),您不希望添加任何content_type
验证并允许人们欺骗内容类型并接收您不希望进入服务器的数据然后添加以下内容:
do_not_validate_attachment_file_type :image
Run Code Online (Sandbox Code Playgroud)
注意:
在content_type
/ matches
options上面根据您的要求指定MIME类型.我刚刚为您提供了一些图像MIME类型.
参考:
如果仍需要验证,请参阅Paperclip:Security Validations.:)
您可能还需要处理此处解释的欺骗验证/sf/answers/1669228501/
Luc*_*ton 19
Just put in your model:
validates_attachment :image, content_type: { content_type: /\Aimage\/.*\Z/ }
Run Code Online (Sandbox Code Playgroud)
https://github.com/thoughtbot/paperclip
需要在 Model 中添加validates_attachment_content_type
轨道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
Run Code Online (Sandbox Code Playgroud)
轨道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
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
61512 次 |
最近记录: |