#<ActionDispatch::Http::UploadedFile:0x007fae8c0bfaa8>的未定义方法`[]'

编程入门 行业动态 更新时间:2024-10-11 09:25:44
本文介绍了#<ActionDispatch::Http::UploadedFile:0x007fae8c0bfaa8>的未定义方法`[]'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在 file_field 列中嵌套了表单,当我尝试创建它时会抛出UserController 中的 NoMethodError#update#

I have nested form with the file_field column, when i try to create it throws NoMethodError in UserController#update undefined method `[]' for #

{ "utf8"=>"✓", "authenticity_token"=>"HFWawKp4RH7+AFV0yQ1cXpzxHDfubKTKkiDiS6QKnJk=", "user"=> { "name"=>"Test", "image_attributes"=> { "picture"=>#<ActionDispatch::Http::UploadedFile:0x3f4e318 @original_filename="Beautiful Sky_thumb.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"user[pics_attributes][pic]\"; filename=\"Beautiful Sky_thumb.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:C:/Users/Alex/AppData/Local/Temp/RackMultipart20120824-4784-5rmxid>> } }, "commit"=>"Save User" }

我的模特

class User < ActiveRecord::Base attr_accessbile: :name, image_attributes has_many :images accepts_nested_attributes_for: images end class Image < ActiveRecord::Base belongs_to :verve_app mount_uploader picture, PictureUploader end

我的控制器

def new @user = User.new @user.images.build end def create @user = User.new(params[:user]) @user.save! end

我的观点

<%= form_for @user do |f| %> <%= f.hidden_field :name, {value: 'Alex'} %> <%= f.fields_for :image_attributes do |image| %> <%= image.file_field :picture %> <% end %> <%= user.submit %> <% end %>

推荐答案

尝试在模型类 :image 而不是 :image_attributes 上调用 fields_for.

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

替换你的 <%= f.fields_for :image_attributes do |image|%> with <%= f.fields_for :image do |i|%>.然后将 image.file_field 改为 i.file_field

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.

更多推荐

#&lt;ActionDispatch::Http::UploadedFile:0x007fae8c0bfaa8&gt;的未定义方法`[]

本文发布于:2023-10-27 23:43:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1534877.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:未定义   方法   ActionDispatch   Http   amp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!