0

I'd like to have just a part of my page updated every second. I know I can get this to work using setInterval in JavaScript but what if I want some logic built into my Rails code? Here's a simple abstraction:

Code in controller:

def lookup_something
  @stuff_to_display = [code to look up a set of records]
end

Then in the view:

<body>    
<div id="update_this"></div>    
</body>

I'd like something to run that action every second and update the div above. I know I've done this before in a previous project with a relatively simple block of code but I can't find out how to do it again for the life of me.

shweta
  • 8,019
  • 1
  • 40
  • 43
Jason Burgett
  • 117
  • 1
  • 10
  • 2
    This depends on the version of Rails. In older versions, there was `periodically_call_remote` but in recent versions this has been removed. I'd recommend checking out this question: http://stackoverflow.com/questions/3661967/rails-3-equivalent-for-periodically-call-remote – Marc Baumbach Feb 12 '13 at 05:09
  • That link did the trick. periodically_call_remote is what I used in the past but it seems to be deprecated in Rails 3. – Jason Burgett Feb 12 '13 at 05:39

1 Answers1

1

If you are willing to use jQuery, the $.ajax call is fairly straightforward: From http://api.jquery.com/jQuery.ajax/

setInterval(function() {
     $.ajax({
        url:"url/to/lookup_something",
        success:function(result){
            // do something here to 
            // $("#update_this")
        }
    });
}, 1000); // every second
Steve Wilhelm
  • 6,200
  • 2
  • 32
  • 36