Ruby on rails 回形针::错误::缺少Rails 4的RequiredValidator错误

Ruby on rails 回形针::错误::缺少Rails 4的RequiredValidator错误,ruby-on-rails,ruby,ruby-on-rails-4,paperclip,Ruby On Rails,Ruby,Ruby On Rails 4,Paperclip,当我试图用我的rails博客应用程序使用回形针上传时,我遇到了这个错误。 不确定它在说“MissingRequiredValidatorError”时指的是什么 我认为通过更新post_参数并赋予它:image就可以了,因为创建和更新都使用post_参数 Paperclip::Errors::MissingRequiredValidatorError in PostsController#create Paperclip::Errors::MissingRequiredValidatorErro

当我试图用我的rails博客应用程序使用回形针上传时,我遇到了这个错误。 不确定它在说“MissingRequiredValidatorError”时指的是什么 我认为通过更新post_参数并赋予它:image就可以了,因为创建和更新都使用post_参数

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

请让我知道我是否可以补充额外的资料来帮助您帮助我。

回形针版本4.0开始,所有附件都需要包括内容类型验证、文件名验证,或者明确声明它们都不需要

如果不执行任何操作,曲别针将引发
曲别针::错误::丢失RequiredValidatorError
错误

在您的情况下,您可以在
Post
模型中添加以下任何一行,指定
已附加文件:image

选项1:验证内容类型 -或-另一种方式

validates_attachment :image, content_type: { 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"] }
是使用正则表达式验证内容类型

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

选项2:验证文件名 选项3:不验证 如果出于某些疯狂的原因(可能是有效的,但我现在想不出),您不希望添加任何
内容\u类型
验证,并允许人们伪造内容类型并在您的服务器上接收您不期望的数据,那么请添加以下内容:

do_not_validate_attachment_file_type :image

注意:

在上面的
内容类型
/
匹配项
选项中,根据您的要求指定MIME类型。
我刚刚为您提供了一些图像MIME类型

参考资料:

如果仍然需要验证,请参阅。)


您可能还必须处理此处解释的欺骗验证

只需将其放入您的模型中:

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

我也无法让这些解决方案中的任何一个起作用。我尝试了回形针3.1,但没有成功。然后,我的应用程序不断告诉我,我的图像文件扩展名未被批准,即使它们是jpg


我终于发现3.5.1版成功了。

需要在模型中添加验证附件内容类型

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
class用户{:medium=>“300x300>”,:thumb=>“100x100>”},:default\u url=>“/images/:style/missing.png”
验证\u附件\u内容\u类型:头像,:内容\u类型=>/\Aimage\/.\Z/
结束
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
class用户{:medium=>“300x300>”,:thumb=>“100x100>”},:default\u url=>“/images/:style/missing.png”
验证\u附件\u内容\u类型:头像,:内容\u类型=>/\Aimage\/.\Z/
结束

确保您的帖子模型如下所示

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

也可以使用新的助手样式定义验证程序,例如,
验证附件:图像、状态:true、内容类型:{content\u类型:[“image/jpg”、“image/jpeg”、“image/png”]}
@rawonstack+1感谢您提出的替代方案。)经过一点调整后,我在答案中加入了这一点<代码>存在:true
验证不是强制性的,因此我已排除了该部分。回形针在PDF文件上载的情况下也会抛出“缺少必需的验证程序错误”。解决方法是:首先安装“GhostScript”,然后在内容类型中添加“application/pdf”。我真的不建议
do\u not\u validate\u attachment\u file\u type
。正如Rdocs所说:多亏了Egor Homakov的一份报告,我们已经采取措施防止人们欺骗内容类型,并将您不希望的数据发送到服务器上。我之所以不进行内容验证,是因为附件不是由用户创建的,而是由系统进程创建的。回形针是S3存储的便利层。所以我想说的是,可以通过升级版本Sort来解决这个问题。我从最新版本开始,现在是4.2.1。没有运气,然后再次没有运气与3.1(我发现这里建议)。其他地方的人(我不记得在哪里)建议了3.5.1,这对我很有用。很高兴知道这个@zire
class Post < ActiveRecord::Base
    has_attached_file :photo
    validates_attachment_content_type :photo, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
end