Reputation: 259
I am trying to show a user by id using the "link to"
in my users_controller.rb
I have a method show as shown below:
def show
@user=User.find(params[:id])
end`
my route to this controller method is:
get 'users/:id/show' => 'users#show', as: :show
and the link to instruction is:
<%= link_to 'show it', show_path(id: user.id) %>
the generated error is:
undefined local variable or method `user'
I also tried this syntax:
<%= link_to 'show it', show_path(@user.id) %>
but also I had this error:
undefined method `id' for nil:NilClass
but when I tried this :<%= link_to 'show it', show_path(1) %>
it worked fine I am using rails 4 and I realy don't know where is the problem
Upvotes: 0
Views: 51
Reputation: 1447
where do you have
<%= link_to 'show it', show_path(@user.id) %>
?
I mean, it should be probably in users controller and index.html.erb, called by index action. If so, you didn't set @user variable. You can for example in controller set:
@users = User.all
and then in your template:
<% @users.each do |user| %>
<%= link_to 'show it', show_path(user.id) %>
<% end %>
Upvotes: 1