Percent Completion with an Ajax Call


I have a 'rails-based' search engine that displays the results of an ActiveRecord call as a collection rows of results (pretty standard stuff w/ rails). e.g. a user can search for "chunky bacon" and they might get 15,829 results.

I'd like to give the user the option to download the ENTIRE result-set (all 15,829 records) as a csv file via an ajax call. this all works well (I'm using FasterCSV and I have a form_remote_tag function call in my results.rhtml file).

<% form_remote_tag :url => {   :action => 'download_results_to_csv'},   :before => '"busy_csv");',   :loading => '' #???   :complete => 'Element.hide("busy_csv");',   :update => 'csv_link_div' do -%>

e.g. the User clicks a button, a "busy_csv" element is displayed while the CSV file is being built. Once the CSV file is built, the "busy_csv" element is hidden and the user is presented with a link to download the newly created CSV file.

HERE'S THE "PROBLEM" Some of these csv files can become quite large and it may take up to 60 seconds to build them. I'd like the user to be aware that the file build is "in-progress" with information that is MORE SPECIFIC than an 'ajax busy' signal.

QUESTION: Is there a way of using the ":loading" key (or something else) from the function, form_remote_tag, to present the "% progress" that has been made in building the file to the user? e.g. It would be nice if the user could see a periodically updated HTML element that said "1000 rows out of 15829 built"..."2000 rows of 15829 built", etc. etc.

My controller function looks like this:

def download_results_to_csv   best = Event.find_by_search_terms(params[:search_term])"/home/bl/Desktop/bl/public/files/zzzz.csv", "w", {:col_sep => "$"}) do |csv|     csv << {|c| c[:name]}     best.each_with_index do |row, i|       csv << {|c| row.send(c[:accessor])}       #i've tried assigning an instance variable here that could be grabbed from the rhtml file, but this doesn't seem to work       @rows_loaded = i if i % 1000 == 0     end   end end

Any ideas on how to show "% progress"??


Use backgroundrb to split off the process. The use a Periodical Updater in your webpage to execute a controller action that queries backgroundrb’s progress.

In short, you’re using Rails to give the user information based on a long running process delegated to backgroundrb instead of locking up a mongrel (or whatever you might be using to serve your app) without any means of progress tracking.

Best regards

Peter De Berdt

thanks! precisely what i was looking for!