Search code examples
jqueryruby-on-railsajaxlong-polling

Rails polling with jquery


I have an index page that lists all users and their statuses (available or unavailable), every user can change his/her status anytime. What I am trying to do is real-time polling to display the most up to date statuses. I have done the Jquery polling function and it works (tried it with a simple alert and it displays the alerts every 10 seconds). Now I don't know what to include in my index.js.erb file, that code that's supposed to refresh the div that includes the users and their statuses. I have this :

$("#users-container").replaceWith("<%= escape_javascript(render @users) %>")

To replace the old users and theirs statuses with the new newer ones, but this is not working, the page is not changing at all.

Any suggestions?

Here is my index.html.erb

<div id="users-container">
  <%= render @users %>

</div>

_user.html.erb

<p> 
    <%= name_with_status(user.full_name, user.status, user.id) %>
    </p>

Where name_with_status is just a helper that displays the name, status.


Solution

  • You might want to try using .html() instead of .replaceWith(). In your case, you want to replace the contents of #users-container, but not the entire element itself.

    So you can do something like this:

    $("#users-container").html("<%= escape_javascript(render @users) %>")