SessionsController#中的NoMethodError销毁未定义的方法`忘记'为nil:NilClass

时间:2015-03-16 00:48:03

标签: ruby-on-rails ruby railstutorial.org

完成Rails教程M. Hartl Ch 8登录,注销

当我试图退出我的应用程序时,我收到了问题标题中给出的错误消息。

我对此非常陌生,但我认为这意味着forget中定义的models/user.rb实例方法在SessionsHelper中调用user出于某种原因,没有。这是我在SessionsHelper中讨论的片段:

#Found in SessionHelper.  Forgets persistent session
def forget(user)
    user.forget
    cookies.delete(:user_id)
    cookies.delete(:remember_tolken)
end

以下是forget实例方法的定义:

#Found in class models/user.rb
def forget
  update_attribute(:remember_digest, nil)
end

我不知道为什么在SessionsHelper中调用时用户会为nil。以下是完整文件,包括SessionsControllerSessionsHelperUser

我希望我很清楚并使用正确的术语。如果我需要澄清,请告诉我。

class SessionsController < ApplicationController
  def new
  end

  def create
    user = User.find_by(email: params[:session][:email].downcase)
    if user && user.authenticate(params[:session][:password])
        log_in user # Log the user in and redirect to the user's showpage
        params[:session][:remember_me] == '1' ? remember(user) : forget(user)
        redirect_to user # same as user_url(user)
    else
        # Show error message
        flash.now[:danger] = 'Wrong email or password' 
        render 'new'
    end
    end

    def destroy
    log_out # if logged_in?    # not logging out
    redirect_to root_url
  end
end

XXXXXXXXXX

module SessionsHelper

# Logs in given user
def log_in(user)
    session[:user_id] = user.id
end

# Remembers a user in a persistant session
def remember(user)
    user.remember
    cookies.permanent.signed[:user_id] = user.id
    cookies.permanent[:remember_token] = user.remember_token
end

#Forgets persistent session
def forget(user)
    user.forget
    cookies.delete(:user_id)
    cookies.delete(:remember_tolken)
end


# Returns the current logged-in user (if any)
def current_user
    if (user_id = session[:user_id]) # CAREFUL! (not ==). Read if session of user_id exists
        @current_user ||= User.find_by(id: user_id)
    else (user_id = cookies.signed[:user_id])
        user = User.find_by(id: user_id)
        if user && user.authenticated?(cookies[:remember_token])
            log_in(user)
            @current_user = user
        end
    end
end

# Returns true if the user is logged-in, else false
def logged_in?
    !current_user.nil?
end

# logs out current user
def log_out
    session.delete(:user_id)
    forget(current_user)
    @current_user = nil
end
end

XXXXXXXXXX

class User < ActiveRecord::Base
attr_accessor :remember_token

  before_save {email.downcase!}
  validates :name, presence: true, length: {maximum: 50}
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
  validates :email, presence: true, length: {maximum: 255},
                    format: { with: VALID_EMAIL_REGEX },
                    uniqueness: {case_sensitive: false}

  has_secure_password
  validates :password, length: {minimum: 6}

  # Returns the hash digest of the given string. Used to make hashed password of user in fixture model for login integration testing                                                        
  def User.digest(string)
    cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
                                                  BCrypt::Engine.cost
    BCrypt::Password.create(string, cost: cost)
  end

  # Returns random token for remembering users via cookies
  def User.new_token
    SecureRandom.urlsafe_base64
  end

  # Remembers a user in the database for use in persisten sessions
  def remember
    self.remember_token = User.new_token
    update_attribute(:remember_digest, User.digest(remember_token))
  end

  # Returns true if the given token matches the digest
  def authenticated?(remember_token)
    return false if remember_digest.nil?
    BCrypt::Password.new(remember_digest).is_password?(remember_token)
  end

  # Forgets a user
  def forget
    update_attribute(:remember_digest, nil)
  end

end

谢谢

2 个答案:

答案 0 :(得分:1)

current_user方法返回nil,这就是将错误进一步抛出。

您的current_user方法中存在拼写错误。将else更改为elsif,它很可能会解决问题。

如果不能解决问题,请确保方法与Listing 8.57 of the rails tutorial中的方法完全相同;同样如下:

# Returns the user corresponding to the remember token cookie.
def current_user
  if (user_id = session[:user_id])
    @current_user ||= User.find_by(id: user_id)
  elsif (user_id = cookies.signed[:user_id])
    user = User.find_by(id: user_id)
    if user && user.authenticated?(cookies[:remember_token])
      log_in user
      @current_user = user
    end
  end
end

答案 1 :(得分:1)

您的log_out方法会在忘记用户之前删除会话。这会导致错误,因为forgetcurrent_user作为参数,current_user使用会话来确定当前用户是谁:

    if (user_id = session[:user_id]) # This will attempt to assign user_id based on the value of session[:user_id], but will fail since the session was deleted

您需要在忘记用户后删除会话。 从此处更改log out方法:

def log_out
    session.delete(:user_id)
    forget(current_user)
    @current_user = nil
end

对此:

def log_out
    forget(current_user)
    session.delete(:user_id)
    @current_user = nil
end