views:

152

answers:

3

I am using multiple file field in a view to upload multiple files. I would like to update the user of progress while uploading. I tried using something like this

def create
params[:upload_data].each do |file|
unless (file =="" or file== nil)
@photo = Photo.create({:approved => false, :user_id => @current_user.id})
@result = "Uploading #{file.original_filename}"
render :update do |page|
page.insert_html(:bottom, 'progress', :partial => 'partials/result')
end
#... perform upload end

It works fine if I remove the feedback to the user. However it fails with a multiple render error when it is in. I was wondering if anyone knows any way of doing this.
Thank You

+2  A: 

You can't call render multiple times. Render should be used only when the controller method will return.

To get this to work, you will need to change how you are doing things to make requests, probably using ajax. Then each upload will update something on the page, and javascript will execute the next "upload this file, then update" call.

Edit: I'm not a frontend guy, so I can't give you actual working code for this, but it would be something like this. Currently, you are sending a several files as parameters into the controller. You need to change this so that the backend is only handling one file at a time. This means changing the frontend to do separate requests to the backend. You can't do this with just an html form -- you'll need javascript. The controller will render an rjs to send back the update "processed file xyz", the client side code will fire off another upload until all files have been uploaded.

Kevin Peterson
Thanks but how can I make request that will provide feedback? Any examples? Thanks
Anark
+1  A: 

I've used this with great success: SWFUpload

It's a flash control that does the uploading, you provide the html front-end, whether you use text or a % bar.

Daniel Beardsley
+1  A: 

If you are using Workling as your background worker it can store progress information that you can then use to update a progess bar on your page using javascript.

railsninja