I'm close ... very close ... I can just upload individual files ... but when I change the type of my form file_field to :multiple => true , so I can upload multiple images at once, my uploaded files are wrapped in an array ... and the magic of accepts_nested_attributes_for is lost.
Edit: After a more detailed study, I wonder if I even have to worry about accepts_nested_attributes_for ? Maybe I just need to have file_field, :multiple => true in my Gallery form (as opposed to a nested form), and then in my create action create a new gallery and then skip every element of the params[:gallery][:photos_attributes]["0"][:image] manually, so to speak, by calling @gallery.photos.create for each element.?!? Seems cumbersome ... but all I can think of.
Hope someone with a lot of experience with Rails can listen ...
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 resources :galleries do resources :photo, :only => [:create, :destroy] end
Galleriescontroller
def new @gallery = Gallery.new @gallery.photos.build respond_to do |format| format.html
Meltemi
source share