RailsGrunt

a noob working the ruby railroad.

ruby rails upload multiple files using acts as attachment

March 14th, 2007

this question seems to pop up in the rails forums at least twice a week. so i guess this may be a beneficial post to folks out there looking to simply add more than 1 file field in a form. remember, the grunt is new to ror so any improvements to this post, please drop me a message.

so far this is what i did.

follow techno weenie tutorial here for creating your model that will store the photos.

In our example, lets say you have a foodrecipe model and a food_photos model. you would do “script/generate attachment_model food_photos”

Next, add your foodrecipe_id to your migration file for your foodphotos.

Next, add your relationships to your model. Therefore, foodrecipe has_many :foodphotos, and foodphotos model, belongs_to foodrecipe.

Next in the action that calls your form, instantiate as many files you would like to add. for example in the dvdcover example, you do @dvd_cover = DvdCover.new. so in our example you can do

@myrecipe = Foodrecipe.new

@myPhoto1 = Foodphoto.new

@myPhoto2 = Foodphoto.new etc…

now in your form, you can add the file fields.

for example in the dvdcover example you would do this..

<% form_for :dvd_cover, :url => { :action => ‘create’ }, :html => { :multipart => true } do |f| -%>

<%= f.file_field :uploaded_data %>

<%= submit_tag :Create %>

<% end -%>

but now, you can do this. (i am sure there is a much better way but this is what i know so far)

<% form_for :myrecipe , :url => { :action => ‘create’ }, :html => { :multipart => true } do |f| -%>

...stuff for your recipe here

<%= file_field “myPhoto1” , “uploaded_data” %>

<%= file_field “myPhoto2” , “uploaded_data” %> ...etc.

<%= submit_tag :Create %>

<% end -%>

now in your create action, build how a has_many is built by doing the following

@foodrecipe = Foodrecipe.new(params[:myrecipe])

@foodrecipe.save

@foodrecipe.foodphoto.build[params[:myPhoto1]]

@foodrecipe.foodphoto.build[params[:myPhoto2]]

that should be able to save your multiple files. this is probably the bruteforce way, so if this can be made better, please drop me a message. thanks!

3 Responses to “ruby rails upload multiple files using acts as attachment”



  1. March 14th, 2007 at 05:47 PM
    Fred
    said:
    Thanks for posting this. I'll give it a try. Ideally, for me, the number of photos would be dynamic based on a user's input. So, I'll have to tweak this a bit.

  2. March 14th, 2007 at 06:29 PM
    railsgrunt
    said:
    hi fred, thanks for visiting. check out this thread for dynamic amount of pictures. http://beast.caboo.se/forums/2/topics/870 basically from what i understand, you can make a rjs call to create new file field, and store all the pictures in a hash. when the hash gets sent to the action, loop through the hash and build each picture. i plan on working on this soon...

  3. March 15th, 2007 at 02:10 PM
    railsgrunt
    said:
    opps, got my understanding wrong, not hash but array. check out the code at http://beast.caboo.se/forums/2/topics/870

Sorry, comments are closed for this article.