我嵌套形式與是,file_field柱,當我嘗試創建它拋出 NoMethodError在UserController中#更新 未定義的方法`[] 「爲#未定義的方法`[]」爲#<ActionDispatch :: HTTP :: UploadedFile的:0x007fae8c0bfaa8>
{
"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 %>
幫我解決這個問題。