【问题标题】:Paperclip::Errors::MissingRequiredValidatorError with Rails 4Rails 4 中的 Paperclip::Errors::MissingRequiredValidatorError
【发布时间】:2014-03-20 19:13:25
【问题描述】:

我在我的 rails 博客应用程序中尝试使用回形针上传时遇到此错误。 不确定当它说“MissingRequiredValidatorError”时它指的是什么 我认为通过更新 post_params 并给它 :image 就可以了,因为 create 和 update 都使用 post_params

Paperclip::Errors::MissingRequiredValidatorError in PostsController#create
Paperclip::Errors::MissingRequiredValidatorError

Extracted source (around line #30):

def create
  @post = Post.new(post_params)

这是我的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    

这是我的帖子助手

module PostsHelper
  def post_params
    params.require(:post).permit(:title, :body, :tag_list, :image)
  end
end

如果我可以补充额外的材料来帮助你,请告诉我。

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4 paperclip


    【解决方案1】:

    Paperclip version 4.0 开始,所有附件都必须包含content_type 验证file_name 验证,或明确声明它们'也不会有。

    如果您不执行任何操作,Paperclip 会引发 Paperclip::Errors::MissingRequiredValidatorError 错误。

    在您的情况下,您可以将以下任何一行添加到您的 Post 模型中,之后指定 has_attached_file :image

    选项 1:验证内容类型

    validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
    

    -或-另一种方式

    validates_attachment :image, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png", "image/gif"] }
    

    -或-另一种方式

    是使用 regex 来验证内容类型。

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

    @LucasCaton's answer

    选项 2:验证文件名

    validates_attachment_file_name :image, :matches => [/png\Z/, /jpe?g\Z/, /gif\Z/]
    

    选项 3:不验证

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

    do_not_validate_attachment_file_type :image
    

    注意:

    在上面的content_type/matches 选项中根据您的要求指定 MIME 类型。我刚刚提供了一些图像 MIME 类型供您开始使用。

    参考:

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

    您可能还必须处理此处解释的欺骗验证https://stackoverflow.com/a/23846121

    【讨论】:

    • 验证器也可以使用新的辅助样式来定义,例如。 validates_attachment :image, presence: true, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png"] }
    • @rawonstack +1 感谢您提出替代方案。 :) 经过一些调整,我已将其包含在答案中。 presence: true 验证不是强制性的,所以我排除了该部分。
    • Paperclip 在上传 PDF 文件时也会抛出“缺少必需的验证器错误”。解决方法是:首先安装“GhostScript”,然后将“application/pdf”添加到 content-type。
    • 我真的不推荐do_not_validate_attachment_file_type 。正如 Rdocs 所说:感谢 Egor Homakov 的报告,我们已采取措施防止人们欺骗 Content-Types 并将您不期望的数据获取到您的服务器上。
    • 我不进行内容验证的疯狂原因是因为附件不是由用户创建的,而是由系统进程创建的。 Paperclip 是 S3 存储的便利层。
    【解决方案2】:

    只需放入您的模型:

    validates_attachment :image, content_type: { content_type: /\Aimage\/.*\Z/ }
    

    https://github.com/thoughtbot/paperclip

    【讨论】:

      【解决方案3】:

      需要在Model中添加validates_attachment_content_type

      Rails 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

      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
      

      【讨论】:

      • 很高兴知道这个@zire
      【解决方案4】:

      确保您的帖子模型看起来像这样......

      class Post < ActiveRecord::Base
          has_attached_file :photo
          validates_attachment_content_type :photo, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
      end
      

      【讨论】:

        【解决方案5】:

        我也无法使用这些解决方案。我尝试了 Paperclip 3.1,但我的应用程序一直告诉我我的图像文件扩展名未获批准,即使它们是 jpg。

        我终于在 3.5.1 版本中找到了成功。

        【讨论】:

        • 所以想说升级版本可以解决这个问题
        • 有点。我从最新版本开始,现在是 4.2.1。那里没有运气,然后 3.1 也没有运气(我发现这里建议)。其他地方的某个人(我不记得在哪里)建议使用 3.5.1,这对我有用。
        猜你喜欢
        • 2016-01-14
        • 1970-01-01
        • 2014-02-25
        • 2016-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-07
        相关资源
        最近更新 更多