我靠近......很接近......我能够上传单个文件就好了......但是,当我改变我的窗体的类型file_field来:multiple => true,所以我可以在我的一次上传的文件被包裹在一个上传多张图片array ...并且'accepts_nested_attributes_for`的'magic'丢失了.
编辑:
经过更多的考试,我想知道我是否还需要打扰accepts_nested_attributes_for?也许我应该只file_field, :multiple => true在我的Gallery表单中(而不是嵌套表单),然后在我的创建操作中创建新的库,然后params[:gallery][:photos_attributes]["0"][:image]手动循环遍历数组中的每个元素,可以这么说,调用@gallery.photos.create每个元素.?!?看起来很麻烦......但是我能想到的就是这些.
希望有更多Rails经验的人可以加入......
PARAMS:
{"utf8"=>"?",
"authenticity_token"=>"9jXvIwcllct7UyUfo6cvhEucQf2u3SY50SuaCLtFO4c=",
"gallery"=>{
"name"=>"First Gallery",
"photos_attributes"=>{"0"=>{
"image"=>[
#<ActionDispatch::Http::UploadedFile:0x00000104b78978
@original_filename="first_test_image.jpg",
@content_type="image/jpeg",
@headers="Content-Disposition: form-data; name=\"gallery[photos_attributes][0][image][]\"; filename=\"first_test_image.jpg\"\r\nContent-Type: image/jpeg\r\n",
@tempfile=#<File:/var/folders/bQ/bQYZC2ukFZCvbKzEDGRtJE+++TI/-Tmp-/RackMultipart20110622-4459-vz78ee>>,
#<ActionDispatch::Http::UploadedFile:0x00000104b78950
@original_filename="second_test_image.jpg",
@content_type="image/jpeg",
@headers="Content-Disposition: form-data; name=\"gallery[photos_attributes][0][image][]\"; filename=\"second_test_image.jpg\"\r\nContent-Type: image/jpeg\r\n",
@tempfile=#<File:/var/folders/bQ/bQYZC2ukFZCvbKzEDGRtJE+++TI/-Tmp-/RackMultipart20110622-4459-1jzhhyg>>
]
}
}
}, "commit"=>"Save", "action"=>"create", "controller"=>"galleries"}
#app/models/gallery.rb
class Gallery < ActiveRecord::Base
has_many :photos, :dependent => :destroy
accepts_nested_attributes_for :photos
end
#app/models/photo.rb
class Photo < ActiveRecord::Base
belongs_to :gallery
mount_uploader :photo, PhotoUploader
end
#config/routes.rb …Run Code Online (Sandbox Code Playgroud)