Search code examples
ruby-on-railsruby-on-rails-3link-to

user_path(user) undefined local variable or method `user'


I'm trying to add a Profile page for the users of my App.

I followed railscasts 250 & 274 to set up my user authentication & added the below to get a user profile.

users/show.html.erb

<div class="page-header">
  <h1><%= @user.name %> Profile</h1>
</div>

<p>
  <b>email:</b>
  <%= @user.email %>
</p>

layouts/_navbar.html.erb

<%= link_to "View your Profile", user_path(user) %>

users_controller.rb

  def show
    @user = User.find(params[:id])
  end

This throws back a undefined local variable or method 'user' error.

So I tried adding both of these lines to my application_controller.rb :

@user = User.all

&

@user = User.all.find(params[:id])

But these returned the following error respectively

undefined local variable or method `user'

&

undefined local variable or method `params'

I have resources :users in my routes.rb file & I've also tried adding get 'users/:id', :to => 'users#show', :as => 'user' without any success.


Solution

  • To go to the show page of the user that is currently logged in:

    <%= link_to "View your Profile", current_user %>
    

    Leave the show action as it was and the routes just with resources :users