Topic: [SOLVED] Updating a part inside a page without reloading

I'm currently adding a new feature on an old rails app (version 2.1), and I would want to do something similar to this:

I have a matrix table that needs to be updated/refresh every now and then (probably by a certain minute). Can this be done in ruby on rails without refreshing the page?

I've used periodically_call_remote but I would want to use another approach since periodically_call_remote is already deprecated and even if I'm still using an old version, I think it would be more better to use another method instead.

Any help would be appreciated. Thanks

Last edited by ajibanda (2011-12-13 22:54:16)

Re: [SOLVED] Updating a part inside a page without reloading

Yea,  get a javascript timer plugin, I use query.timers.1.1.2.js

There are a lot of ways to do this,  all would require slight tweaking if you ever migrate to 3.1.  Since binding to page element events is now the preferred AJAX methodology, might as well at least use bind now.

Bind a timer to something,  probably the div containing the table.

The when the timer fires,  send an ajax request to a rails action (update_table?) expecting AJAX, put a render.js in the format block. 

def update_table
  respond_to do |format|
    format.js
  end

Then create an RJS file, i.e. update_table.js.rjs.  To keep it brief, I'll assume the table is created in the original view by a partial called _table.html.erb

update_table.rjs:

page.replace_html 'div_id_of_table' , :partial=> 'table'
Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: [SOLVED] Updating a part inside a page without reloading

thanks for the help BradHodges

May I ask how can I do the ajax call via ruby on rails or should I use just an ordinary AJAX call?

Re: [SOLVED] Updating a part inside a page without reloading

Ok I do have this code (I'm using a button clicked event for now just for checking):

for my view

<a href="javascript:;" id="bg_button">THIS</a>

<div id="testDiv">
  <%= render :partial => "main/schedule" %>
</div>

<script>
  $("#bg_button").click(function(){
        new Ajax.Request("<%= url_for(:controller => "main", :action => "update_table") %>", {
      method: 'get',
      parameters: { date_start: '<%= @date_start %>' },
      onSuccess: function(res){
                alert("result");
            },
      onError: function(error){
        alert("error");
      }
    });
    });
</script>

On my main controller

   def update_table
  respond_to do |format|
    format.js
  end
   end

and a an RJS file located at main/view

page.replace_html 'testDiv', :partial=> "main/schedule"

Now upon clicking the button, I get the result alert and see in the console that update_table was called, the problem is nothing happened.

Re: [SOLVED] Updating a part inside a page without reloading

No need, I already solved it. update_table should be update_table.js.erb and NOT update_table.js.rjs

My fault! tongue Thanks again!