0

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.

Holly
  • 7,462
  • 23
  • 86
  • 140
  • `<%= link_to "View your Profile", user_path(@user) %>` doesn't work for you? – ted Apr 08 '13 at 09:51
  • unless you did something really funky the `params` variable should never be undefined... –  Apr 08 '13 at 09:58

2 Answers2

2

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

Zippie
  • 6,018
  • 6
  • 31
  • 46
  • Thankyou Zippie, this worked :). I don't see how current_user can be a route though, seems like magic – Holly Apr 08 '13 at 10:08
  • any variable that contains a object from your database can be used to redirect you to the objects show page. The `current_user` is a object of the `User` class so it can be used to redirect you to the user's show page :) – Zippie Apr 08 '13 at 10:10
1

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 }) %>