Pregunta

I am trying to render the user detail on index page when user is clicked.

I am having the list of users in index page like

<% @users.each do |user| %>
   <%= link_to user.name, '', {:id => user.id, :name => 'user', :remote => true}
<% end %>

In my javascript

$('#mydiv').html("<%= escape_javascript(render(:partial => '/users/show', :locals => {:id => #{params['id']}})) %>"

but I couldn't able to render the user details, because param 'id' is not passing to this page.

How to get this param and render the partial in the index page when user is clicked.

¿Fue útil?

Solución

In this case you should review your code a little. You should directly call the show method in your link and edit your controller to have it responding to JavaScript:

View code:

<%= link_to user.name, user_path(user), remote: true %>

Controller code:

def show
    @user = User.find(params[:id])
    respond_to do |format|
        format.html
        format.js
    end
end

And create a new view called users/show.js.erb

$("#mydiv").html("<%= escape_javascript(render(:partial => '/users/user_show', :locals => {:user => @user})) %>");

This view is calling a partial view where you can render all your user data. This view is called users/_user_show.html.erb

<div class="myuser">
    <%= user.name %>
</div>

Hope that helps

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top