iprog.com

uploading multiple files with attachment_fu

i was recently sharing with someone how to make multiple uploads work with the attachment_fu plugin. as an aside, attachment_fu is the successor to acts_as_attachment. if you were planning to use acts_as_attachment, upgrade your plans and use attachment_fu instead.

multiple file uploads are actually fairly straight forward.

first, in the view:

1<%= file_field_tag 'attachment_data[]' %>

put as many of these in the view as desired. it’s also easy to cause an arbitrary number of them to be added via javascript by the user’s browser.

then, in the controller:

1@attachment = Attachment.new
2params[:attachment_data] ||= []
3params[:attachment_data].each do |file|
4  @attachment = Attachment.create({:uploaded_data => file}) unless file == ""
5end

Attachment is my model for uploaded file—substitute as appropriate. as this isn’t a complete how-to for attachment_fu, i won’t get into configuring attachment_fu or the model here.

that’s it. that wasn’t so painful, eh?

21 comments