Need help refactoring a controller, perhaps add threading?

My problem is occurring at the exec() statement, it completely takes
over and freezes everything until it's finished at which time the
render_text inserts the generated chart into my div. This process only
takes 4 or so seconds but in that 4 or so seconds I would like to put a
message inside the div saying "Your chart is being built" with my
spinner animation under it so the user knows that something is going on.

I put the exec in a Thread.new{} but I'm not too familiar with how
threads function so my render_text line to put up the image was firing
before the chart was built. How would I correctly make a look to check
thread.alive? to see if it's finished or not?

Most web frameworks frown upon user created/managed threads.

This would be fairly esoteric imo, but if you really realy insist,
look into BackgrounDRb for running the job asynchronously. Would need
some kind of AJAX push library to feed the results to clients when
done.

Ultimately I would like for the user to click the link, populate the div
with the text and spinner, generate the chart and place it in the div
when finished. I also wasn't sure if this was a job for an RJS template
but the few screencasts I watched didn't give me the impression I could
do what I needed with an RJS template (which I could be very well
wrong).

Hmm.. I'd go with two partials.

When clicking, load the 'work in progress' div. Once that completes,
start a second remote function to fetch the real contents, e.g.
through remote_function's :completed callback.

-Isak

Isak Hansen wrote:
>> before the chart was built. How would I correctly make a look to check
>> thread.alive? to see if it's finished or not?
>>
>
> Most web frameworks frown upon user created/managed threads.
>
> This would be fairly esoteric imo, but if you really realy insist,
> look into BackgrounDRb for running the job asynchronously. Would need
> some kind of AJAX push library to feed the results to clients when
> done.
>
>
>> Ultimately I would like for the user to click the link, populate the div
>> with the text and spinner, generate the chart and place it in the div
>> when finished. I also wasn't sure if this was a job for an RJS template
>> but the few screencasts I watched didn't give me the impression I could
>> do what I needed with an RJS template (which I could be very well
>> wrong).
>
> Hmm.. I'd go with two partials.
>
> When clicking, load the 'work in progress' div. Once that completes,
> start a second remote function to fetch the real contents, e.g.
> through remote_function's :completed callback.
>
>
> -Isak

That sounds like the best option to me (logically anyway).

Could you provide a reference or the basic calls I would use to
accomplish calling the functions and working with the :completed
callback?

See ActionView::Helpers::Prototype*/JavaScript*/Scriptaculous in the API docs.

Daniel already explained how you'd write actions that update certain
parts of the page, see ActionController::Base#render and
ActionView::Base for more info.

I think something like this should work:

<%= link_to_remote("Load data", :url => {:action =>
"replace_div_with_spinner"}, :complete => remote_function(:url =>
{:action => "replace_div_with_real_contents"}) ) %>

HTH,
Isak