# Refreshing the view from the controller

**URL:** <https://www.ruby-forum.com/t/refreshing-the-view-from-the-controller/134453>\
**Category:** Rails\
**Created:** [April 10, 2008, 11:58pm UTC](https://www.ruby-forum.com/t/refreshing-the-view-from-the-controller/134453 "2008-04-10T23:58:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [April 10, 2008, 11:58pm UTC](https://www.ruby-forum.com/t/refreshing-the-view-from-the-controller/134453/1 "2008-04-10T23:58:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [April 11, 2008, 12:03am UTC](https://www.ruby-forum.com/t/refreshing-the-view-from-the-controller/134453/2 "2008-04-11T00:03:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [April 11, 2008, 1:05am UTC](https://www.ruby-forum.com/t/refreshing-the-view-from-the-controller/134453/3 "2008-04-11T01:05:08Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [April 11, 2008, 2:03am UTC](https://www.ruby-forum.com/t/refreshing-the-view-from-the-controller/134453/4 "2008-04-11T02:03:47Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [April 11, 2008, 2:29am UTC](https://www.ruby-forum.com/t/refreshing-the-view-from-the-controller/134453/5 "2008-04-11T02:29:04Z")

</div>

thanks, I’ll look into this

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [April 11, 2008, 2:22am UTC](https://www.ruby-forum.com/t/refreshing-the-view-from-the-controller/134453/6 "2008-04-11T02:22:56Z")

</div>

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.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

–  
Jonathan

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [April 11, 2008, 10:56pm UTC](https://www.ruby-forum.com/t/refreshing-the-view-from-the-controller/134453/7 "2008-04-11T22:56:23Z")

</div>

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](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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
