简体   繁体   中英

undefined local variable or method 'current_user'

I'm currently going through a RoR tutorial ( http://railstutorial.org/chapters/sign-in-sign-out#sec:signin_success is the relevant section) which seems to be fairly good, although I've run into the following problem when trying to view the sample site.

Extracted source (around line #10):

7:          <li><%= link_to "Home", root_path %></li>
8:          <li><%= link_to "About", about_path %></li>
9:          
10:             <% if signed_in? %>
11:                 <li><%= link_to "Profile", current_user %></li>
12:                 <li><%= link_to "Sign out", signout_path, :method => delete %></li>
13:             <% else %>

As you can see, the issue is stemming from my method "signed_in?" which is supposed to check if the user has logged in or not by checking whether the current_user variable is set (I've included the rest of the code from the helper to give a context, apologies):

module SessionsHelper

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

  def sign_out
    cookies.delete[:remember_token]
    current_user = nil
  end

  def current_user= (user)
    @current_user ||= user_from_remember_token
  end

  def signed_in?
    !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

From my understanding, .nil? is a method that checks whether or not an object has been defined and therefore the object being undefined shouldn't generate an error but rather return false? I searched the tutorial for all cases of current_user (before checking to see if anyone else had this problem with little success) and my code seems correct so I'm a little confused and if anyone is able to help me understand the way Ruby variables are supposed to be accessed and why my code isn't working I'd be most grateful.

Edit:

I'm not sure if it's important with scope as I'm just beginning both Rails and Ruby, however the helper SessionsHelper is being used by my Users controller and views (it's included in my Applications controller)

I ran in to this same issue & it was for the same reason. You overlooked part of the instructions on 'Listing 9.16'.

def current_user= (user)
  @current_user ||= user_from_remember_token
end

You were supposed to change this to the following.

def current_user
  @current_user ||= user_from_remember_token
end

You'll also want to change all of the instances of *self.*current_user to *@*current_user.

Once you do this the error(s) are resolved.

Make sure you have the following code in the SessionHelper

def current_user=(user)
    @current_user = user
end

def current_user
    @current_user ||= user_from_remember_token
end

The nil? method is not going to check whether a variable or method is defined. It is solely checking whether it is defined as a nil object or not. Ruby walks up the ancestors chain for SessionsHelper and finally determines that current_user is not defined anywhere (it will eventually end at Kernel#method_missing) and then throws an error. The quickest way to solve the problem would be:

#app/helpers/sessions_helper.rb
def current_user
  @current_user ||= false
end

I asked a friend, and he corrected my errors. I think a large part of my mistake came from not being completely familiar with variable scope in Ruby and forgetting that everything is an object and therefore the method current_user=(user) was overriding the assignment function.

My friend's solution was to change the scope of current_user to an instanced variable (so it can be properly used), and change the function curent_user=(user) to a simple get_current_user function in order to determine if the current user exists in the cookie.

The final changed code is as follows:

#app/helpers/sessions_helper.rb

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

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

  def get_current_user
    @current_user ||= user_from_remember_token
  end

  def signed_in?
    !get_current_user.nil?
  end

#app/views/layouts/_header.erb
<% if signed_in? %>
                <li><%= link_to "Profile", get_current_user %></li>
                <li><%= link_to "Sign out", signout_path, :method => :delete %></li>
            <% else %>
                <li><%= link_to "Sign in", signin_path %></li>
            <% end %>

As you can see the variable in my header partial has also been changed to reflect the method used in the helper to obtain the user.

Going to start reading some basic Ruby guides so next time I get in over my head I have an idea of where to start fixing it :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM