Ruby-on-rails – undefined local variable or method `current_user’

deviseruby-on-rails

I am building my first Ruby on Rails app (ruby 2.0.0p353) by following the teamtreehouse.com guide.

I am using devise 3.2.2 devise and in application.html.erb I want to use devise's "current_user" in order to display the name of the logged in user.

Here the code where I am using "current_user":

    <ul class="nav navbar-nav pull-right">
      <li><%= link_to current_user.first_name, "#" %></li> 
      <li><%= link_to "Log Out", destroy_user_session_path, method: :delete %></li> 
    </ul>

routes.db:

Treebook::Application.routes.draw do
   devise_for :t_busers
   resources :statuses
   root to: 'statuses#index'
end

However when running my application I am always getting the following error:

undefined local variable or method `current_user' for #<#<Class:0x007f98b1d0edb8>:0x007f98b1cff6d8>

I am getting the same error with other methods of devise. I get the error no matter if I am signed in or not signed out. The sign-up, edit etc. of devise seems to work fine. I have tried putting "before_filter :authenticate_user!" into application_controller.rb as indicated in the documentation of devise but that doesn't work either.

Is there anything I need to do in order to use the helpers of devise in a view?

Best Answer

From your routes file, the helper should be

current_t_buser

From the documentation of devise

Notice that if your devise model is not called "user" but "member", then the helpers you should use are:

before_filter :authenticate_member!

member_signed_in?

current_member

member_session

Related Topic