Uploading multiple files at once to a Rails application using Carrierwave (HTML5)

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 # new.html.erb format.json { render json: @gallery } end end ... def create @gallery = Gallery.new(params[:gallery]) respond_to do |format| if @gallery.save format.html { redirect_to @gallery, notice: 'Gallery was successfully created.' } format.json { render json: @gallery, status: :created, location: @gallery } else format.html { render action: "new" } format.json { render json: @gallery.errors, status: :unprocessable_entity } end end end 
+7
source share
2 answers

You can hack a little params array, for example:

 aux = [] params[:gallery][:photos_attributes][:image].each do |f| aux << {:image => f} end params[:post][:photos_attributes] = aux @gallery = Gallery.new(params[:gallery]) 

I have a similar problem, I know this is an ugly hack, but it works for me.

+4
source

Ditch accepts_nested_attributes_for and instead add this to your gallery model.

 def photos=(attrs) attrs.each { |attr| self.photos.build(:image => attr) } end 

Also, make sure the photos are in your gallery, accessible attributes, if you protect against mass assignment. Otherwise, you will not get the destination of the hash array of photos from your parameters. I.e.

 attr_accessible :field1, field2, :photos 
+2
source

All Articles