Refreshing the view from the controller

Hello,
I was wondering if it was possible to do this… I have a really long
task that is written in the controller. I’d like to update the user
viewing the webpage by printing out things like “initializing…” then
after it completes something printing “completed blah blah” or whatever.
I guess the best way to say this is, could I refresh the view from the
controller?

Thanks,

  • Jeff M.

On 10 Apr 2008, at 22:58, Jeff M. wrote:

Hello,
I was wondering if it was possible to do this… I have a really long
task that is written in the controller. I’d like to update the user
viewing the webpage by printing out things like “initializing…” then
after it completes something printing “completed blah blah” or
whatever.
I guess the best way to say this is, could I refresh the view from the
controller?

Some sort of PeriodicalUpdater ? (see the periodically_call_remote
helper)

Fred

I’m pretty sure you can only send one response per request.

The way I’d do this is to start the processing using something like
backgroundrb and then use ajax to make periodic calls to the server
saying “is it done yet?”.

On Apr 11, 10:03 am, Frederick C. [email protected]

hmmm… I can’t seem to find any good information or examples using
periodically_call_remote and I have absolutely zero experience with
Ajax… I have an idea, though - is there a way to open a popup window
and tell it to refresh from the controller or load another page into the
popup from the controller?

thanks, I’ll look into this

No, you can still only send one response per request. From what you
are saying you want to send responses to the browser while the server
is working? I don’t think you can do this.

I think you’re either going to need to look into ajax (in particular
XMLHttpRequest), or wait till the server finishes it’s job, and can
return the complete output in one go.

I don’t think there is a helper method for an XMLHttpRequest, but it’s
not too difficult to understand. For a similar problem in my app, I
did this:

javascript function

function poll_server(id) {
new Ajax.Request(’<%= url_for :action => :check_processing %>/’ +
id, {asynchronous:true});
}

  • I would call poll_server after the page had loaded.
  • The “check_processing” method either returned a result, or some
    javascript that would wait 5 seconds, then check processing again:

check_processing.rjs

ruby/rails rjs template

if @finished_processing

do something cool

else
page.delay( 5 ) do
page << “poll_server( ‘#{params[:id]}’ );”
end
end

On Fri, Apr 11, 2008 at 12:03 PM, Jeff M.
[email protected] wrote:


Jonathan

Yes, you can do this with ajax. Or, if you just want something simple
to work:

http://www.html-reference.com/META_httpequiv_refresh.htm

-Danimal

P.S. Since the request-response pair is single-threaded, you can’t
just “poll” it unless the initial request to the controller causes it
to be passed off to a background process or another thread or
something like that. Otherwise, the initial response back from the
controller to the view will “block” until the controller is done. But
if you pass of the heavy task to a background process (i.e
backgroundrb or something like that) then have another action like
“status” that the browser can periodically “poll”, then you could use
AJAX or the EQUIV-REFRESH on that status.

On Apr 10, 6:29 pm, Jeff M. [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs