Topic: [SOLVED] how to refresh div using ajax? (rails 3.2)

Hello,

I have a div I would like to update periodically, based on either an interval or a model attribute change using javascript.
This is running on rails 3.2.
What would be the easiest way to go about this?

Thanks in advance,

Matt

Last edited by djronin47 (2012-03-02 14:32:56)

Re: [SOLVED] how to refresh div using ajax? (rails 3.2)

http://railsforum.com/viewtopic.php?id=47954

Read my last post (including the link it links to) in that topic and then let me know if you have any questions.

Remember to edit your topic title and add "[SOLVED]" if your question has been answered!

Follow me!

Re: [SOLVED] how to refresh div using ajax? (rails 3.2)

All of that though seems to point to user interaction causing updates to occur, this needs to be without user intervention.

Thanks

Re: [SOLVED] how to refresh div using ajax? (rails 3.2)

The only thing you would change would be:

1. Ignore the part about calling the function via Rails' "remote"
2. Add to your javascript a simple timeout that will call the ajax function you create.

All you need to do is create the timeout, have it call a certain route, the route the URL called to a function in your controller. When the controller's function is called, have it render the data (in your special ajax.html.erb file). Lastly, have the ajax call (from your javascript file) place the data into the required div.

Does that help?

Remember to edit your topic title and add "[SOLVED]" if your question has been answered!

Follow me!

Re: [SOLVED] how to refresh div using ajax? (rails 3.2)

I am trying to use something like this

$(document).ready( function(){
  setInterval(function(){
    $('#refreshable').load(location.href+"#refreshable"); }, 3000);})

but instead of refreshing the content of the div, its replacing the content of the div with an entire page.

This is being used on a show action only.

Re: [SOLVED] how to refresh div using ajax? (rails 3.2)

Show me the code for the controllers, routes, and views related to this problem (doesn't have to be full files, but snippets of related code). I'll help you get it working. It sounds like either your controller is rendering the wrong file, or your ajax view file isn't setup correctly.

Remember to edit your topic title and add "[SOLVED]" if your question has been answered!

Follow me!

Re: [SOLVED] how to refresh div using ajax? (rails 3.2)

This is the view code for show.js.erb

$(function(){
  var html = "<%= escape_javascript(render('showpage')) %>";
  $("#refreshable").prepend(html)
});

The controller simply has added to it a format.js in the respond_to block

and the route hasn't been changed

Re: [SOLVED] how to refresh div using ajax? (rails 3.2)

If you're wanting to replace the previous data inside of "#refreshable", you would want to use

$("#refreshable").html(html)

Also, in your previous post, try $.ajax instead of .load(). http://api.jquery.com/jQuery.ajax/ -- On the "success" of the ajax call, the "data" will be what is in your rendered file (in your case, what format.js renders).

Lastly, what is showpage in your escape_javascript()?

Remember to edit your topic title and add "[SOLVED]" if your question has been answered!

Follow me!

Re: [SOLVED] how to refresh div using ajax? (rails 3.2)

That was a partial to be rendered that I was monkeying with to see if it'd work.

Re: [SOLVED] how to refresh div using ajax? (rails 3.2)

and so use $("#refreshable").html(html) as the js.erb file?

Re: [SOLVED] how to refresh div using ajax? (rails 3.2)

This what you are suggesting for the refresh js file?

$(document).ready( function(){
  setInterval(function(){
    $('#refreshable').ajax(location.href+"#refreshable"); }, 10000);})

Re: [SOLVED] how to refresh div using ajax? (rails 3.2)

More like this (untested)

$(document).ready(function(){

  // The ajax function
  var replaceContent = function(){
    $.ajax({
      type : 'GET',   //or POST, PUT, etc.
      url : 'url_to_load',
      dataType : 'html',
      success : function(data){
        $('#refreshable').html(data);
      },
      error : function(XMLHttpRequest, textStatus, errorThrown) {
        alert('Error!');
      }
    });

  }


  //Interval
  setInterval('replaceContent', 10000);

});

It will load the HTML that is returned in your .js.erb file that is called. It will then replace that response-html into #refreshable

Make sense?

Last edited by wesf90 (2012-02-29 18:03:06)

Remember to edit your topic title and add "[SOLVED]" if your question has been answered!

Follow me!

Re: [SOLVED] how to refresh div using ajax? (rails 3.2)

the refresh function works, however, still the content of the div ends up being like the entire <% yield %> from the layout's application.html.erb for the show action
so instead of just updating the div with the content of the div, its putting a smaller version of the whole page within itself

Re: [SOLVED] how to refresh div using ajax? (rails 3.2)

What is the URL you're calling with ajax?

Remember to edit your topic title and add "[SOLVED]" if your question has been answered!

Follow me!

Re: [SOLVED] how to refresh div using ajax? (rails 3.2)

I'm hoping to simply pass from rails into ajax the path for the id of the item the page is for. but can't get that to work either.

Re: [SOLVED] how to refresh div using ajax? (rails 3.2)

I'll probably need to see some of your code before I can suggest the next step. It's just hard to blind-guess from here on out. Give me some snippets of the related files and I'll try and help

Remember to edit your topic title and add "[SOLVED]" if your question has been answered!

Follow me!

Re: [SOLVED] how to refresh div using ajax? (rails 3.2)

well i figured in the link_to I'd need a :remote => true, but then it never changes anything on screen.
the div I am replacing is nested within several other divs. i.e.

<div class="maindiv">
  <div class="container">
     <div id="refreshable">
               content
     </div>
   </div>
<div>

I wonder if this is part of it

Re: [SOLVED] how to refresh div using ajax? (rails 3.2)

In addition the current way to access said show action where I want the refresh to take place on, is accessed from the index page.

Re: [SOLVED] how to refresh div using ajax? (rails 3.2)

Could you post the code you have for your javascript, controller, and .js.erb file? Doesn't have to be everything, just related parts. That would help a lot, maybe one tiny thing is missing, or one tiny thing needs added smile

Remember to edit your topic title and add "[SOLVED]" if your question has been answered!

Follow me!

Re: [SOLVED] how to refresh div using ajax? (rails 3.2)

the controller for show

def show
  @item = Item.find(params[:id])

  respond_with(:item)
end

the current js timer

$(document).ready(function() {
  var replacementContent = function() {
    $.ajax( {
     type : 'GET',
     url : '#item_path',
     dataType : 'html',
     success : function(data) {
         $('#refreshable').html(data);
     },

  error: function(XMLHttpRequest, testStatus, errorThrown) {
     alert('Error!');
   }

   });

}
setInterval("replaceContent()", 10000);
});

the current show.js.erb

$("#refreshable").html("<%= escape_javascript( render(:partial => "showpage")) %>");