Wes Gamble
2006-Mar-29 22:31 UTC
[Rails] Posting multiple files - how to set up params correctly
All, I have a page where I am allowing the user to specify multiple image files that are related to one HTML document. I want to end up with an array of image files that I can then assign into an array within the controller. I''m trying to set up my file_field correctly but am having trouble. I''m not clear on how "the multiple input tags with the same name turning into an array of parameters" thing works? I''ve tried: <%= file_field(''document'', ''image[]'') %> <%= file_field(''document[]'', ''image[]'') %> and when I do debug(params), I never see more than one image in the "document" hash. What am I doing wrong here? Can one use the file_field helper to generate multiple file selection elements successfully? =================================================== Here is my form: <%= form_tag( { :action => ''upload_images'' }, { :multipart => ''true'' } )%> <TABLE> <% for image in images do %> <TR> <TD> <%= image.path.split(''/'').last[0..39] %>: <% if ((! image.path.nil?) && (image.path.length > 40)) %> <%= h("...") %> <% end %> </TD> <TD> <%= file_field(''document'', ''image'') %> </TD> </TR> <% end %> </TABLE> <P> <%= submit_tag "Upload Images" %> </P> <%= end_form_tag %> Thanks, Wes -- Posted via http://www.ruby-forum.com/.
Sean Cribbs
2006-Mar-29 22:47 UTC
[Rails] Posting multiple files - how to set up params correctly
Unfortunately, the form helpers are not equipped to handle arrays well, but they do their job very well otherwise. Since file upload tags are simple, I think you can be trusted to do this one yourself: <input type="file" name="document[image][0]" /> <input type="file" name="document[image][1]" /> ... ad nauseum Cheers, Sean Cribbs Web Services - KCKCC>>> weyus@att.net 3/29/2006 3:47 PM >>>All, I have a page where I am allowing the user to specify multiple image files that are related to one HTML document. I want to end up with an array of image files that I can then assign into an array within the controller. I''m trying to set up my file_field correctly but am having trouble. I''m not clear on how "the multiple input tags with the same name turning into an array of parameters" thing works? I''ve tried: <%= file_field(''document'', ''image[]'') %> <%= file_field(''document[]'', ''image[]'') %> and when I do debug(params), I never see more than one image in the "document" hash. What am I doing wrong here? Can one use the file_field helper to generate multiple file selection elements successfully? =================================================== Here is my form: <%= form_tag( { :action => ''upload_images'' }, { :multipart => ''true'' } )%> <TABLE> <% for image in images do %> <TR> <TD> <%= image.path.split(''/'').last[0..39] %>: <% if ((! image.path.nil?) && (image.path.length > 40)) %> <%= h("...") %> <% end %> </TD> <TD> <%= file_field(''document'', ''image'') %> </TD> </TR> <% end %> </TABLE> <P> <%= submit_tag "Upload Images" %> </P> <%= end_form_tag %> Thanks, Wes -- Posted via http://www.ruby-forum.com/. _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060329/a2ed6ab8/attachment-0001.html