Rails 4中的回形针-禁用参数的强参数错误 [英] Paperclip in Rails 4 - Strong Parameters Forbidden Attributes Error

查看:113
本文介绍了Rails 4中的回形针-禁用参数的强参数错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Having a problem with a Paperclip upload in Rails 4 - failing on ForbiddenAttributesError (strong parameters validation). Have the latest paperclip gem and latest rails 4 gems.

我有一个模型"Image",模型中带有附件"upload":

I have a model "Image" with an attached file "upload" in the model:

has_attached_file :upload, :styles => { :review => ["1000x1200>", :png], :thumb => ["100x100>", :png]}, :default_url => "/images/:style/missing.png"

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

The image model was created with a scaffold, and I added paperclip migrations. The form partial was updated to use

f.file_field :upload

该表格会生成一组看似典型的回形针参数,其中的图像参数包含上载内容.我还在图像模型中传递了transaction_id,因此应该允许它.就是这样-图片和交易ID.

the form generates what appears to be a typical set of paperclip params, with the image param containing the upload. I am also passing a transaction_id in the image model, so it should be permitted. But that's it - the image and the transaction ID.

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

I expected to be able to write the following in my controller to whitelist my post - but it failed:

def image_params
  params.require(:image).permit(:transaction_id, :upload)
end

所以我变得更加明确-但这也失败了:

So I got more explicit - but that failed too:

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

我对Rails 4没有向我展示在开发环境中出现ForbiddenAttributesError失败的情况感到有些沮丧-应该显示该错误,但没有显示-这将是简化开发的一个不错的补丁.也许其他所有人都得到了我所缺少的东西!非常感谢您的帮助.

I'm a bit frustrated that Rails 4 is not showing me what ForbiddenAttributesError is failing on in a development environment - it is supposed to be showing the error but it does not - would be a nice patch to ease development. Or perhaps everyone else is getting something that I am missing! Thanks much for the help.

推荐答案

我了解现在这里发生的情况-希望您能对此有所帮助.我正在从Rails 3项目中移植代码,但是错过了创建图像的行:

I understand what happened here now - and will leave this up in the hope it helps someone else. I was porting code from a rails 3 project and missed the line that created the image:

@image = current_user.images.new(params[:image])

在导轨4中,这是不正确的(我相信).我更新为

In rails 4 this is incorrect (I beleive). I updated to

@image = current_user.images.new(image_params)

这解决了我的问题.

这篇关于Rails 4中的回形针-禁用参数的强参数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆