# Percent Completion with an Ajax Call

**URL:** <https://www.ruby-forum.com/t/percent-completion-with-an-ajax-call/113689>\
**Category:** Rails\
**Created:** [September 3, 2007, 11:12pm UTC](https://www.ruby-forum.com/t/percent-completion-with-an-ajax-call/113689 "2007-09-03T23:12:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 3, 2007, 11:12pm UTC](https://www.ruby-forum.com/t/percent-completion-with-an-ajax-call/113689/1 "2007-09-03T23:12:19Z")

</div>

Hi,

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 =\> ‘Element.show(“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])

FasterCSV.open("/home/bl/Desktop/bl/public/files/zzzz.csv", “w”,  
{:col\_sep =\> “$”}) do |csv|  
csv \<\< @@columns.map {|c| c[:name]}  
best.each\_with\_index do |row, i|  
csv \<\< @@columns.map {|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”??

TIA!!!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 3, 2007, 11:22pm UTC](https://www.ruby-forum.com/t/percent-completion-with-an-ajax-call/113689/2 "2007-09-03T23:22:21Z")

</div>

On 03 Sep 2007, at 23:11, removed\_email\_address@domain.invalid wrote:

> 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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 4, 2007, 2:00am UTC](https://www.ruby-forum.com/t/percent-completion-with-an-ajax-call/113689/3 "2007-09-04T02:00:30Z")

</div>

thanks! precisely what i was looking for!
