Holly
Holly

Reputation: 7752

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.

Upvotes: 0

Views: 2638

Answers (2)

Zippie
Zippie

Reputation: 6088

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

Upvotes: 2

Jonny Wright
Jonny Wright

Reputation: 83

I think the problem is in your layouts/_navbar file. You'll need to define user.

Use the helper: <%= link_to "View your Profile", user_path(current_user) %>

Or: <%= link_to "View your Profile", url_for({ :controller => 'users', :action => 'show', :id => current_person.id }) %>

Upvotes: 1

Related Questions