Trying to connect to user profile page, rails

时间:2016-04-04 17:47:02

标签: ruby-on-rails ruby devise

I'm trying to create a navbar link to the profile page of whoever is logged in. I'm using devise for authentication. It keeps throwing various errors. Heres what i have so far.

in the navbar

<li><% yield (:profile)%><%= link_to "My Account", profile_path(user)%></li>

in my routes

 get '/:id', to: 'users#show', as: :profile

my users_controller

class UsersController < ApplicationController


    def show
      @user = User.find(params[:id])
      if current_user !=@user
        redirect_to root_url

  end

end
end

with this setup its throwing this error

"undefined local variable or method `user' for #<#:0x007fdb615f4940>"

any tips on fixing?

EDIT now its throwing an error anytime i click on another link in the navbar... heres an exampleenter image description here

1 个答案:

答案 0 :(得分:2)

it should be current_user instead of user

<li><% yield (:profile)%><%= link_to "My Account", profile_path(current_user)%></li>

Try to change user profile route

get 'user/:id', to: 'users#show', as: :profile