gpt4 book ai didi

ruby-on-rails - SessionsController 中的 NoMethodError#destroy undefined method `forget' for nil :NilClass

转载 作者:数据小太阳 更新时间:2023-10-29 09:03:45 26 4
gpt4 key购买 nike

通过 M. Hartl 的 Rails 教程第 8 章登录、注销

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

我对此很陌生,但我认为这意味着在 models/user.rb 中定义并在 SessionsHelper< 中调用的 forget 实例方法 正在 user 上调用,由于某种原因为 nil。这是我在 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 中调用时 user 会是 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

谢谢

最佳答案

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

    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

您需要在忘记用户之后删除 session 。更改您的 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

关于ruby-on-rails - SessionsController 中的 NoMethodError#destroy undefined method `forget' for nil :NilClass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29068050/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com