Rails中的回形针4 - 强参数禁止属性错误

Esk*_*im0 10 paperclip paperclip-validation strong-parameters ruby-on-rails-4

在Rails 4中出现Paperclip上传问题 - ForbiddenAttributesError失败(强参数验证).拥有最新的回形针宝石和最新的rails 4宝石.

我有一个模型"图像",在模型中附加文件"上传":

has_attached_file :upload, :styles => { :review => ["1000x1200>", :png], :thumb => ["100x100>", :png]}, :default_url => "/images/:style/missing.png"
Run Code Online (Sandbox Code Playgroud)

图像模型是用脚手架创建的,我添加了回形针迁移.表单部分已更新为使用

f.file_field :upload
Run Code Online (Sandbox Code Playgroud)

表单生成一组典型的回形针参数,图像参数包含上传.我也在图像模型中传递了transaction_id,因此应该允许它.但就是这样 - 图像和交易ID.

我希望能够在我的控制器中编写以下内容以将我的帖子列入白名单 - 但它失败了:

def image_params
  params.require(:image).permit(:transaction_id, :upload)
end
Run Code Online (Sandbox Code Playgroud)

所以我更明确 - 但也失败了:

def image_params
  params.require(:image).permit(:transaction_id, :upload => [:tempfile, :original_filename, :content_type, :headers])
end
Run Code Online (Sandbox Code Playgroud)

我有点沮丧的是Rails 4没有告诉我ForbiddenAttributesError在开发环境中失败了 - 它应该显示错误但它没有 - 将是一个很好的补丁来简化开发.或者也许其他人都得到了我所缺少的东西!非常感谢你的帮助.

Esk*_*im0 9

我理解现在发生的事情 - 并且希望能帮助别人.我从rails 3项目移植代码并错过了创建映像的行:

@image = current_user.images.new(params[:image])
Run Code Online (Sandbox Code Playgroud)

在轨道4中,这是不正确的(我相信).我更新到了

@image = current_user.images.new(image_params)
Run Code Online (Sandbox Code Playgroud)

这解决了我的问题.

  • 你最后把你的params.permit()放到了什么让它工作? (3认同)
  • 在我的情况下,我有一个模型"图像"与回形针附件'image_file'.回形针迁移在数据库中创建了几个列,例如image_file_file_name等,但您只需要执行params.require(:image).permit(:image_file)以允许回形针发挥其魔力. (3认同)