undefined method `[]' for #<ActionDispatch::Http::UploadedFile:0x007fae8c0bfaa8>

假如想象 提交于 2019-12-01 19:16:11

Try calling fields_for on the model class :image rather than on :image_attributes.

Replace your <%= f.fields_for :image_attributes do |image| %> with <%= f.fields_for :image do |i| %>. Then change image.file_field to i.file_field

See if this helps, or at least produces new error for us to work from.

Dan Andrei Diac

In the View, change :image_attributes to :images The rest of your code is correct and this change should make it work. If you want to ave multiple instances of the children (images) just build that many in your controller.

Here's a related ticket where you can see examples: Plural for fields_for has_many association not showing in view

Don't forget that your model class should contain the hook for the uploader :-)

mount_uploader :picture, PictureUploader

Is it possibly because the form needs to read:

<% f.fields_for :images_attributes do |image| %> (Note the plural)

It's my understanding that in the call to x_attributes, x needs to match how the attributes were declared in the model. In this case, it was set up accepts_nested_attributes_for :images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!