# Update rails variable from database

**URL:** <https://www.ruby-forum.com/t/update-rails-variable-from-database/187742>\
**Category:** Rails\
**Created:** [April 28, 2010, 5:46pm UTC](https://www.ruby-forum.com/t/update-rails-variable-from-database/187742 "2010-04-28T17:46:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bill-h](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ebca7d/32.png) [@bill-h](https://www.ruby-forum.com/u/bill-h)\
**Post date:** [April 28, 2010, 5:46pm UTC](https://www.ruby-forum.com/t/update-rails-variable-from-database/187742/1 "2010-04-28T17:46:29Z")

</div>

Hi all.

Im using ROR and am trying to get some data from a database into a web  
page then periodically update this information say every 5 seconds. Ive  
got the content working statically and have the div refreshing at  
intervals, the only problem now is passing that new information from the  
database into the variable.I guess I have to use AJAX for this. Anyone  
got any ideas?  
Thanks

---

<div class="post-metadata">

**Author:** ![bill-h](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ebca7d/32.png) [@bill-h](https://www.ruby-forum.com/u/bill-h)\
**Post date:** [April 28, 2010, 6:18pm UTC](https://www.ruby-forum.com/t/update-rails-variable-from-database/187742/2 "2010-04-28T18:18:41Z")

</div>

On Apr 28, 4:46 pm, William H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi all.
> 
> Im using ROR and am trying to get some data from a database into a web  
> page then periodically update this information say every 5 seconds. Ive  
> got the content working statically and have the div refreshing at  
> intervals, the only problem now is passing that new information from the  
> database into the variable.I guess I have to use AJAX for this. Anyone  
> got any ideas?

Rails has a helper (periodically\_call\_remote) that will generate  
javascript that calls an action at the specified interval and updates  
the specified div with whatever is rendered by the action

Fred

---

<div class="post-metadata">

**Author:** ![bill-h](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ebca7d/32.png) [@bill-h](https://www.ruby-forum.com/u/bill-h)\
**Post date:** [April 28, 2010, 7:09pm UTC](https://www.ruby-forum.com/t/update-rails-variable-from-database/187742/3 "2010-04-28T19:09:18Z")

</div>

Frederick C. wrote:

> Rails has a helper (periodically\_call\_remote) that will generate  
> javascript that calls an action at the specified interval and updates  
> the specified div with whatever is rendered by the action

Also note that the Rails helper is a thin wrapper around Prototype’s  
Ajax.PeriodicalUpdater.

[http://api.prototypejs.org/ajax/ajax/periodicalupdater/](http://api.prototypejs.org/ajax/ajax/periodicalupdater/)

---

<div class="post-metadata">

**Author:** ![bill-h](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ebca7d/32.png) [@bill-h](https://www.ruby-forum.com/u/bill-h)\
**Post date:** [April 29, 2010, 3:21am UTC](https://www.ruby-forum.com/t/update-rails-variable-from-database/187742/4 "2010-04-29T03:21:09Z")

</div>

Frederick C. wrote:

> On Apr 28, 4:46ï¿½pm, William H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Hi all.
> > 
> > Im using ROR and am trying to get some data from a database into a web  
> > page then periodically update this information say every 5 seconds. Ive  
> > got the content working statically and ï¿½have the div refreshing at  
> > intervals, the only problem now is passing that new information from the  
> > database into the variable.I guess I have to use AJAX for this. Anyone  
> > got any ideas?
> 
> Rails has a helper (periodically\_call\_remote) that will generate  
> javascript that calls an action at the specified interval and updates  
> the specified div with whatever is rendered by the action
> 
> Fred

Would you be able to take a look at the code below. Ive done what you’ve  
suggested and from the Rails console I can see that the data is been  
queried at a regular interval and the div is refreshing but the new data  
is not being inserted. FYI the value of @uptime is taken from an SNMP  
query in the controller.

Thanks

```auto
<% content_for :subtitle do %>Log Collector Server Status<%end%> <p></p>
<% content_for :error do %>
<%=javascript_include_tag 'application'%>
<%=javascript_include_tag 'prototype'%>
  <% if @collectorstatus == TRUE%>
      <div class="success">Network Log Collector Is Online</div>
      <div class="info">IP: 192.168.1.204</div>
      <div class="info2">Uptime: <%=@uptime%></div>
      <script src="http://ajax.googleapis.com/ajax/
      libs/jquery/1.3.0/jquery.min.js"></script>
      <script>
      var auto_refresh = setInterval(
      function()
      {
      $('.info2').fadeOut('fast').fadeIn("fast");
      }, 1);
      </script>

      <%=periodically_call_remote(:url => 'status', :frequency => '5', 
:update => '.info2')%>
  <%else if @collectorstatus == FALSE%>
      <div class="error">Network Log Collector Is Offline!</div>
  <%else%>
 <%end%>
<%end%>
<%end%>
<% content_for :content do %>
  <% if @collectorstatus == TRUE%>
    <div id="gauges2">
     <% visualization "ProcessorGauge", "Gauge", :width =>500, :height 
=>200, :redFrom => 90, :redTo=> 100, :yellowFrom=>80, :yellowTo=> 90, 
:minorTicks=> 5, :html => {:class => "gauge"} do |chart| %>
            <% chart.string "Name" %>
            <% chart.number "Values" %>
            <% chart.add_rows([
                            ["Processor ",@processorusage],
                            ["Mem Used ",@memoryint]
                    ]) %>
    <% end %>
    <% end %>
    </div>
<%end%>

```
