Rectangle 27 0

Rails Paperclip & Multiple File Uploads?

:html => {:multipart => :true }
class Listing < ActiveRecord::Base
  has_many :assets

class Asset < ActiveRecord::Base
  belongs_to :listing
  has_attached_file :picture
has_attached_file :asset1
has_attached_file :asset2
has_attached_file :asset3
has_attached_file :asset4
has_attached_file :asset5

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 of has_attached_file. If you want to attach 5 files, you would need to do something like:

I already do have another model created called "Listing". A listing has_many assets. I already can upload multiple assets, just not through a single "Choose File" field. if anyone has any good examples using uploadify that'd be great. I'm not sure how to do the background JS to process each image and POST them to the server properly.

Just to add to this: i would move the file uploading to ajax, allowing you to gather a list of files from the user and then upload them one at a time in the background: this way it would appear to the user that they have uploaded lots of files at once, even though behind the scenes that's not what's happening. You can also set any other params for how the resulting records might need to be organised on the server. For file upload with ajax (jquery), which can be a bit tricky to get right, see eg

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:

OR, alternatively, you could create another model just to store the files. For example:

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): However, I cannot vouch for how good it is.

So there are a few issues here.

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").

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.

You can add :multiple => "multiple" to your file_field, that enables multiple file selection from one html file input field (at least in Chrome). I haven't solved the problem of managing these uploads properly yet, but I know that post parameter includes an array of the multiple files you have picked and uploaded.

Rectangle 27 0

Rails Paperclip & Multiple File Uploads?

<%= f.file_field :files, multiple: true %>
<%= form_for @dog, html: { multipart: true } do |f| %>
  <%= f.file_field :files, accept: 'image/png,image/jpeg,image/gif', multiple: true %>
<%= end %>
def dog_params
  params.require(:dog).permit files: []
def files=(array = [])
  array.each do |f|
    images.create file: f

Accepted answer says there is no such thing as a multiple file upload in HTML.

If you have the relationship Dog has_many Images and Image has_attachment :file, do this to get multiple images to upload at once:

This allows you to select multiple images and send them as an array.