Candielope
Candielope

Reputation: 39

Rails, Render partials with jquery

I am wondering if there is some kind of way to do this in rails:

$('#fire').click(function(){
    <%= render 'userlist' %>
});

I'm using websocket rails(gem) to send me updates whenever someone enter the page. When they enter, it automatically creates a user model. And I want the userlist to update itself on every screen each time someone enters the page.

Upvotes: 0

Views: 54

Answers (2)

Diego Polido Santana
Diego Polido Santana

Reputation: 1435

Maybe if you call an action from UserController:

$('#fire').click(function() {
      $.ajax({
        type: "GET",
        url: "/users",
        success: function(data) {
          $("#your-table").html(data); //use .html() if you fecth all list from controller
          $("#your-table").append(data); //use .append() - that case you must pass, i.e., a param to specify the last page or 
        }
      });
    });

Upvotes: 1

margo
margo

Reputation: 2927

I'm guessing that rather than re-render the page every time a user enters, you want to add the user to the existing page? You can do that by appending or prepending the newly entered user to the userlist. Let's say you have a ul tag with a class of 'user-list'.

$('#fire').click(function(){
  $(".user-list").append("<li>" + user-name + "</li>")
});

This is dependent on how the new user is being made available. You may want to create a js function to handle updating the page and then you can pass the new use to the function. I would need to know more about the view and how you get the new user.

Upvotes: 0

Related Questions