使用Rails 4的Paperclip :: Errors :: MissingRequiredValidatorError

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

选项1:验证内容类型

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)

- 还是另一种方式

是使用正则表达式来验证内容类型.

例如:要验证所有图像格式,可以如下所示指定正则表达式

@ LucasCaton的回答

选项2:验证文件名

validates_attachment_file_name :image, :matches => [/png\Z/, /jpe?g\Z/, /gif\Z/]
Run Code Online (Sandbox Code Playgroud)

选项3:不要验证

如果出于某些疯狂的原因(可能有效但我现在想不到一个),您不希望添加任何content_type验证并允许人们欺骗内容类型并接收您不希望进入服务器的数据然后添加以下内容:

do_not_validate_attachment_file_type :image
Run Code Online (Sandbox Code Playgroud)

注意:

content_type/ matchesoptions上面根据您的要求指定MIME类型.我刚刚为您提供了一些图像MIME类型.

参考:

如果仍需要验证,请参阅Paperclip:Security Validations.:)

您可能还需要处理此处解释的欺骗验证/sf/answers/1669228501/

  • 验证器也可以使用新的帮助器样式定义,例如.`validates_attachment:image,presence:true,content_type:{content_type:["image/jpg","image/jpeg","image/png"]} (3认同)
  • 我没有进行内容验证的疯狂原因是因为附件不是由用户创建的,而是由系统进程创建的.Paperclip是S3存储的便利层. (3认同)

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


Arv*_*ngh 5

需要在 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)