I am looking for a solution to give the user the ability to upload multiple images through one file_field. I have looked in to options such a Jquery File Upload and Uploadify but have yet to come across good examples with a working solution.
I already have multiple images setup,
has_attached_file :asset,
:styles => { :large => "640x480", :medium => "300x300", :thumb => "100x100" },
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:path => "/:contributor_id/:listing_name/:filename"
Right now I am displaying 5 individual file_fields
def new
@listing = Listing.new
5.times {@listing.assets.build }
respond_to do |format|
format.html # new.html.erb
format.json { render json: @listing }
end
end
I would like to have
<%= f.file_field :asset, :multiple => true %>
That allows the user to select multiple files in their file browser. But how can I process these with a nested model? And get them to upload.
Accepted answer says there is no such thing as a multiple file upload in HTML.
This allows you to select multiple images and send them as an array.
If you have the relationship
Dog has_many Images
andImage has_attachment :file
, do this to get multiple images to upload at once:In your html.erb
In your controller
In your Dog model
This is assuming you're already able to upload one image but want to upgrade to multiple images at once. Notice that wait time will increase.
To help reduce wait time, peep my post on this question related to speed uploading.
So there are a few issues here.
First, Paperclip's
has_attached_file
method isn't an association to many files. It looks like you're trying to build an "asset" as if it's a Rails association. All Paperclip does is put a couple of fields into your table to store some meta-data about the file and you get one attached file per declaration ofhas_attached_file
. If you want to attach 5 files, you would need to do something like:OR, alternatively, you could create another model just to store the files. For example:
That way, you could have multiple assets attached to one listing (you didn't say what the original object was so I just called it "listing").
Second, there is no such thing as a multiple file upload in HTML (and, as such, the
file_field
method doesn't take a:multiple => true
argument. You'll have to use something beyond Rails built-in form handling if you want multiple-file upload. Uploadify is a decent choice (that I've used before). There is a gem that will transform file fields to use uploadify (and will support the:multiple => true
syntax that you want): https://github.com/mateomurphy/uploadify_rails3/wiki. However, I cannot vouch for how good it is.My advice would be to start step-by-step. Uploading via Flash to Rails can be a complicated process that involves dealing with the CSRF meta-tag and other fields in your form. Start by making a form that allows a user to upload one file and stores it through Paperclip. Then maybe break the
has_attached_file
declaration into another model so that you can have 1 or many files associated with a model (as shown in the multi-model code block above). Then try adding Uploadify or another alternative. Ernie Miller has a decent tutorial on integrating Uploadify: http://erniemiller.org/2010/07/09/uploadify-and-rails-3/.To start, remember that
has_attached_file
can only attach one file. When you try calling@listing.assets
there is no "assets". There is an asset. You need to create a separate model yourself and use Rails' associations if you want multiple files.