23

Seems like periodically_call_remote is deprecated in Rails 3, any ideas how to achieve the same functionality?

Omar Ali
  • 8,467
  • 4
  • 33
  • 58

6 Answers6

26

I was searching for the right way to do it with Rails 3 but now I am confident that there is no equivalent to periodically_call_remote in Rails 3. To get the exact same functionality done with jQuery, I used:

$(document).ready(
  function(){
    setInterval(function(){
      $('#mydiv').load('/controller/action');
    }, 3000);
  });
Omar Ali
  • 8,467
  • 4
  • 33
  • 58
  • Trying to implement this, but struggling. If I insert this code in application.js, include javascript defaults in the header, and have a DOM element with id='mydiv', then it should bind automatically, correct? Also, how can I dynamically update the URL, for instance if I have a nested resource so that I need to load '/controller/:id/action', where :id is set dynamically? – Ed Haywood Jun 22 '11 at 03:56
  • To do that, simply add data-item-id to your element and load it with jQuery's .data() and you can definitely do the same with the URL. – Omar Ali Jun 25 '11 at 00:03
5

If you really want to use it, then you can install the legacy plugin which has this method:

http://github.com/rails/prototype_legacy_helper

Rails favors UJS now.

Check out these links for more info on UJS in Rails 3:
http://railscasts.com/episodes/205-unobtrusive-javascript
http://www.simonecarletti.com/blog/2010/06/unobtrusive-javascript-in-rails-3/

I did a short writeup on this in relation to prototype and the link_to_remote tag:
http://www.cowboycoded.com/2010/07/20/replacing-link_to_remote-with-ujs-in-rails-3-prototype/

Hope this helps!

johnmcaliley
  • 11,015
  • 2
  • 42
  • 47
3

Use setInterval to periodically run a function which invokes an AJAX call. If you were doing it with jQuery, it might be something like:

var pollBackend = function() {
  $.getJSON("/foo/bar/poll");
}

setInterval(pollBackend, 5000);

This would attempt to poll the given URL for JSON data every 5 seconds.

Chris Heald
  • 61,439
  • 10
  • 123
  • 137
2

This is what you need for a periodic ajax call, replace some_path with your own RESTful route:

<%= javascript_tag do -%>
$(document).ready(
  function(){
    setInterval(function(){
      $.ajax({
        url: "<%= some_path %>",
        type: "GET",
        dataType: "script"
    });
  }, 60000 );
});
<% end -%>

The index.js.erb file would look like this:

$('#mydiv').html("<%= escape_javascript( render( :partial => 'my_partial' ) ) %>");
gdonald
  • 984
  • 2
  • 12
  • 23
2

If you want to continue using prototype.js, so you can do what follows:

<%= content_for :js do %>
  new Ajax.PeriodicalUpdater("mydiv", 
    "/controller/action", 
    { frequency: 5, 
      method: 'get' })
<% end %>

More about Ajax.PeriodicalUpdater here.

1

This is a very simple solution I came across

<head>
    <script type="text/javascript">         
      setTimeout("location.reload();",20000);
    </script>
</head>