#<ActionDispatch::Http::UploadedFile:0x007fae8c0bfaa8>的未定义方法`[]' [英] undefined method `[]' for #<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.
这篇关于#<ActionDispatch::Http::UploadedFile:0x007fae8c0bfaa8>的未定义方法`[]'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!