Rails教程(M.Hartl)第3版第8章为什么应用忘记登录状态失败?

Rails Tutorial (M.Hartl) 3rd Edition, Chapter 8, Why does the app fail to forget login status?

登录应用程序然后完全关闭浏览器后,我重新打开浏览器,希望被注销。但是我仍然登录?

我在 8.2.3 Changing the layout links 部分,除上述内容外,应用程序的外观和运行均符合预期。代码也与教程中的一样。我认为这些是相关文件:

app/helpers/sessions_helper.rb

module SessionsHelper

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

  # Returns the current logged-in user (if any).
  def current_user
    @current_user ||= User.find_by(id: session[:user_id])
  end

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

app/controllers/sessions_controller.rb

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
      redirect_to user
    else
      flash.now[:danger] = 'Invalid email/password combination'
      render 'new'
    end
  end

  def destroy
  end
end

After logging into the application and then closing the browser completely, I re-open the browser expecting to be logged-out. However I am still logged in?

因为您仍在登录您的应用程序。您的浏览器通过用于跟踪会话的 cookie 记住登录数据。

要注销,您可以使用本教程中介绍的注销 link,或者您可以清理浏览器缓存。

因为您有 cookie,所以您在每个页面会话中都喜欢这种方式,这就是创建方式。它们被存储。如果你想删除,就去配置,删除cookies

通常情况下,session会在您关闭浏览器时被清除,所以在这种情况下您应该会自动注销。不过,某些浏览器无论如何都会记住您的会话,正如 this footnote 中所讨论的,这可能就是这里发生的情况。

根据 Mhartl 的回答,如果您使用 Chrome(我不知道为什么)但是当您通过 firefox 打开您的应用程序时,您将在关闭浏览器后遇到用户未注销的情况, 用户将如第 8 章所述自动注销(再次不确定如何)。