# Rails+Ajax how to auto update information with out user intervention?

**URL:** <https://www.ruby-forum.com/t/rails-ajax-how-to-auto-update-information-with-out-user-intervention/137443>\
**Category:** Rails\
**Created:** [May 13, 2008, 7:49am UTC](https://www.ruby-forum.com/t/rails-ajax-how-to-auto-update-information-with-out-user-intervention/137443 "2008-05-13T07:49:55Z")\
**Posts on this page:** 7\
**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:** [May 13, 2008, 7:49am UTC](https://www.ruby-forum.com/t/rails-ajax-how-to-auto-update-information-with-out-user-intervention/137443/1 "2008-05-13T07:49:55Z")

</div>

Hi, all the examples I find of ajax on rails are mostly the same,  
user clicks on link or button and ajax runs…  
but what about if ajax starts when the page loads the first time  
and keeps running? for example to have a clock in reality what I need  
is to check the number of online users I have in an application and to  
show this value on the page with out a need to reload…

Any place with a good example on how to do this? or a tip or something?  
Thanks

## – [http://rek2.binaryfreedom.info](http://rek2.binaryfreedom.info) [http://www.spboston.org](http://www.spboston.org)

a social revolution is the only feasible route  
to the establishment of socialism.

- Karl Marx -

---

<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:** [May 13, 2008, 7:54am UTC](https://www.ruby-forum.com/t/rails-ajax-how-to-auto-update-information-with-out-user-intervention/137443/2 "2008-05-13T07:54:02Z")

</div>

On Tue, May 13, 2008 at 12:49 AM, rek2 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi, all the examples I find of ajax on rails are mostly the same,  
> user clicks on link or button and ajax runs…  
> but what about if ajax starts when the page loads the first time  
> and keeps running? for example to have a clock in reality what I need  
> is to check the number of online users I have in an application and to  
> show this value on the page with out a need to reload…
> 
> Any place with a good example on how to do this? or a tip or something?  
> Thanks

periodically\_call\_remote

[http://api.rubyonrails.com/classes/ActionView/Helpers/PrototypeHelper.html#M000959](http://api.rubyonrails.com/classes/ActionView/Helpers/PrototypeHelper.html#M000959)

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<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:** [May 13, 2008, 9:08am UTC](https://www.ruby-forum.com/t/rails-ajax-how-to-auto-update-information-with-out-user-intervention/137443/3 "2008-05-13T09:08:11Z")

</div>

Greg D. wrote:

> > Thanks
> 
> periodically\_call\_remote
> 
> [Peak Obsession](http://api.rubyonrails.com/classes/ActionView/Helpers/PrototypeHelper.html#M000959)

Hi thanks for the link… but there is calling a url I need to call the  
variable or a method  
now I have something like this in views:  
Current online users: **1\<%= @online %\>**  
and my method in my controller:

class StatusController \< ApplicationController  
def currently\_online  
@online = Status.count\_by\_sql “SELECT COUNT(\*) FROM status WHERE  
status = ‘online’;”  
end

so I have the info I need auto updated on a variable @online

–  
A Spectre is haunting multinational capitalism–the spectre of free  
information.  
All the powers of ``globalism’’ have entered into an unholy alliance to  
exorcize this spectre:  
Microsoft and Disney, the World Trade Organization, the United States  
Congress and the European Commission.  
Eben Moglen from the FSF.  
[http://emoglen.law.columbia.edu/publications/dcm.html](http://emoglen.law.columbia.edu/publications/dcm.html)

Solidarity:  
[http://www.dailyradical.org](http://www.dailyradical.org)  
[http://www.diarioradical.org](http://www.diarioradical.org)  
[http://www.spboston.org](http://www.spboston.org)

> **[MESSIPOKER - Situs Judi Slot Online Dan Toto 4D Togel Terbaru](https://binaryfreedom.info/)**
>
> MESSIPOKER ialah situs judi slot online terbaik dan toto 4D online terpercaya no 1 di Indonesia yang siap memberikan pelayanan maksimal kepada anda.

---

<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:** [May 13, 2008, 10:35am UTC](https://www.ruby-forum.com/t/rails-ajax-how-to-auto-update-information-with-out-user-intervention/137443/4 "2008-05-13T10:35:41Z")

</div>

On 13 May 2008, at 08:07, rek2 wrote:

> def currently\_online  
> @online = Status.count\_by\_sql “SELECT COUNT(\*) FROM status  
> WHERE  
> status = ‘online’;”  
> end
> 
> so I have the info I need auto updated on a variable @online

First off that method probably belongs in the Status class. Secondly I  
think you’re making this more complicated than it is. All you need is  
a controller than can generated the appropriae bit of output (ie **1\<  
%= @online %\>**). Make sure that that is easily indentifiable in  
your html (eg

…
) then call  
periodically\_update\_remote, telling it to update the onlineusers div.

Fred

---

<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:** [May 13, 2008, 6:27pm UTC](https://www.ruby-forum.com/t/rails-ajax-how-to-auto-update-information-with-out-user-intervention/137443/5 "2008-05-13T18:27:28Z")

</div>

Frederick C.  
escribió:\> On 13 May 2008, at 08:07, rek2 wrote:

> > def currently\_online  
> > a controller than can generated the appropriae bit of output (ie **1\<  
> > %= @online %\>**). Make sure that that is easily indentifiable in  
> > your html (eg
> > 
> > …
> > ) then call  
> > periodically\_update\_remote, telling it to update the onlineusers div.
> 
> Fred

Hi Fred, I had it in the status class but then I had a problem because  
were in the view I have this is in the application layout view, someone  
told me to put it on this controller and to pass a before filter, that  
works fine at least as a non-ajax solution.  
yes yes of course I need to add divisions

 etc and update that  
part is clear, but what do I call from periodically\_update\_remote in  
the action? the method? or the variable? as you saw before my method  
does not print anything just puts the right info into a variable.

Thanks.  
.

## – [http://rek2.binaryfreedom.info](http://rek2.binaryfreedom.info) [http://www.spboston.org](http://www.spboston.org)

a social revolution is the only feasible route  
to the establishment of socialism.

- Karl Marx -

---

<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:** [May 13, 2008, 6:32pm UTC](https://www.ruby-forum.com/t/rails-ajax-how-to-auto-update-information-with-out-user-intervention/137443/6 "2008-05-13T18:32:14Z")

</div>

On 13 May 2008, at 17:26, rek2 wrote:

> > 
> 
> Hi Fred, I had it in the status class but then I had a problem because  
> were in the view I have this is in the application layout view,  
> someone  
> told me to put it on this controller and to pass a before filter,  
> that  
> works fine at least as a non-ajax solution.  
> You need to set @online in the controller. What I mean was have a  
> Status.count\_online method, which a model thing ( and by the way I’d  
> probably use Status.count :all, :conditions =\> [“status = ‘online’”]  
> rather than count by sql.

> yes yes of course I need to add divisions
> 
> etc and update that  
> part is clear, but what do I call from periodically\_update\_remote in  
> the action? the method? or the variable? as you saw before my method  
> does not print anything just puts the right info into a variable.
> 
> periodically\_update\_remote makes ajax requests. The only thing it can  
> call is an action (which should output an appropriate bit of html).
> 
> Fred

---

<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:** [May 14, 2008, 2:32am UTC](https://www.ruby-forum.com/t/rails-ajax-how-to-auto-update-information-with-out-user-intervention/137443/7 "2008-05-14T02:32:48Z")

</div>

I remember the dialog. What Fred is suggesting is what was  
recommended previously. I think the fragmented nature of the  
conversation jumbled the puzzle but all the pieces were there.

On May 13, 12:31 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
