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
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
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: <b>1<%= @online %></b>
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
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 <b>1<%= @online %></b>). Make sure that that is easily indentifiable in your html (eg <div id="onlineusers">...</div>) then call periodically_update_remote, telling it to update the onlineusers div.
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: <b>1<%= @online %></b>
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
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 <b>1< %= @online %></b>). Make sure that that is easily indentifiable in your html (eg <div id="onlineusers">...</div>) 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 <div> 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.
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 <div> 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).
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.