Multiple file inputs on the same form

Hey all,

I’m still sorting out some of the basics here, and I still can’t quite
find an explanation of how the file_data variable (object?) behaves if
I’m going to be putting multiple file upload tags on the same page.
Can someone give me a quick rundown or point me to a link/previous
thread?

Thanks,
Jeff

You must give each of them a unique name and then you’ll get
a param for each of them. Otherwise it won’t work.

If you need this for a real world app you may consider
using swfupload instead, since it’s more comfortable for
the user (but more difficult to implement)

In your view:

File
<%= file_field_tag ‘files[]’ %>

And id suggest putting it in a partial and then getting an ajax link
that renders one each time.So assuming, you put the above lines in a
partial called _file_form.html.erb then you want something like:

<%= link_to_remote( "Add another file
:update => “file_forms”,
:url => { :action => :add_file_form },
:position => “bottom” ) %>

Which calls a controller method calles add_file_form wich is
basically:

def add_file_form
render(:partial => ‘file_form’)
end

Ajax aside, the function that hadles the params should look something
like:

def include_files
params[:files].each_with_index do |file, index|
model = ModelName.new( :uploaded_data => file )
if model.valid?
# save or attach to other or do whatever you need
else
# your data was not valid. so redirect, flash, whatever
end
end
end

please nopte that i am using the plugin attachment_fu to upload and
handle binaries. and my example is used with images, so i have to
validate the files size and type before i save it.

hope it helps