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

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

问题描述

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

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.

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

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"

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

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])

在 rails 4 中这是不正确的(我相信).我更新到

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

@image = current_user.images.new(image_params)

这就解决了我的问题.

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

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