Action Controller Error: undefined local variable or method `current_user'

Newbie learning Rails. I’m currently on Chp9 here:
http://railstutorial.org/chapters/sign-in-sign-out#top

At the end of the tutorial, rails is erroring (see below). being new
to Rails and after having checked the tutorial… How do you resolve
this kind of error. It’s saying current_user is not defined, and it is
supposed to be defined with/Sites/sample_app/app/helpers/
sessions_helper.rb

NameError in Pages#home

Showing /Users/iamme/Sites/sample_app/app/views/layouts/
_header.html.erb where line #6 raised:

undefined local variable or method `current_user’ for #<#<Class:
0x103371258>:0x10336be70>
Extracted source (around line #6):

3:
4:


    5:
  • <%= link_to “Home”, root_path %>

  • 6: <% if signed_in? %>
    7:
  • <%= link_to “Profile”, current_user %>

  • 8: <% end %>
    9:
  • <%= link_to “Help”, help_path %>

  • Trace of template inclusion: app/views/layouts/application.html.erb

    Rails.root: /Users/iamme/Sites/sample_app

    Application Trace | Framework Trace | Full Trace
    app/helpers/sessions_helper.rb:13:in signed_in?' app/views/layouts/_header.html.erb:6:in_app_views_layouts__header_html_erb___1701013666_2174344740_524030’
    app/views/layouts/application.html.erb:10:in
    `_app_views_layouts_application_html_erb__1439570095_2174436720_0’

    ---- END

    sessions_helper.rb CODE:

    module SessionsHelper

    def sign_in(user)
    cookies.permanent.signed[:remember_token] = [user.id, user.salt]
    current_user = user
    end

    def current_user=(user)
    @current_user = user
    end

    def signed_in?
    !current_user.nil?
    end

    def sign_out
    cookies.delete(:remember_token)
    self.current_user = nil
    end

    private

def user_from_remember_token
  User.authenticate_with_salt(*remember_token)
end

def remember_token
  cookies.signed[:remember_token] || [nil, nil]
end

end

— END

thanks

Ok figured it out, the sessions_helper.rb needs to look like this:

module SessionsHelper

def sign_in(user)
cookies.permanent.signed[:remember_token] = [user.id, user.salt]
current_user = user
end

def current_user=(user)
@current_user = user
end

def current_user
@current_user ||= user_from_remember_token
end

def signed_in?
!current_user.nil?
end

def sign_out
cookies.delete(:remember_token)
self.current_user = nil
end

private

def user_from_remember_token
  User.authenticate_with_salt(*remember_token)
end

def remember_token
  cookies.signed[:remember_token] || [nil, nil]
end

end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs