# Multiple file inputs on the same form

**URL:** <https://www.ruby-forum.com/t/multiple-file-inputs-on-the-same-form/141286>\
**Category:** Rails\
**Created:** [June 24, 2008, 11:10am UTC](https://www.ruby-forum.com/t/multiple-file-inputs-on-the-same-form/141286 "2008-06-24T11:10:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [June 24, 2008, 11:10am UTC](https://www.ruby-forum.com/t/multiple-file-inputs-on-the-same-form/141286/1 "2008-06-24T11:10:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [June 24, 2008, 12:36pm UTC](https://www.ruby-forum.com/t/multiple-file-inputs-on-the-same-form/141286/2 "2008-06-24T12:36:52Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [June 24, 2008, 6:54pm UTC](https://www.ruby-forum.com/t/multiple-file-inputs-on-the-same-form/141286/3 "2008-06-24T18:54:17Z")

</div>

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
