完成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。以下是完整文件,包括SessionsController
,SessionsHelper
和User
。
我希望我很清楚并使用正确的术语。如果我需要澄清,请告诉我。
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
谢谢
答案 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
方法会在忘记用户之前删除会话。这会导致错误,因为forget
将current_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