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

Sag*_*dya 2 ruby ruby-on-rails railstutorial.org

登录到应用程序然后完全关闭浏览器后,我重新打开希望注销的浏览器.但是我还在登录?

我在8.2.3节中更改布局链接,除此之外,应用程序按预期运行并运行.代码也是教程中的代码.我认为这些是相关文件:

应用程序/佣工/ 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
Run Code Online (Sandbox Code Playgroud)

应用程序/控制器/ 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
Run Code Online (Sandbox Code Playgroud)

mha*_*rtl 5

通常,session当您关闭浏览器时会清除它,因此在这种情况下您应该自动注销.不过,有些浏览器会记住你的会话,正如本脚注中所讨论的那样,可能就是这里发生的事情.