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

时间:2016-01-22 14:26:24

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

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

我在 8.2.3 Changing the layout links 部分,除此之外,应用程序按预期查看并运行。代码也是教程中的代码。我认为这些是相关文件:

应用/助手/ 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

应用/控制器/ 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

4 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:0)

  

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

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

要注销,您可以使用教程中说明的注销链接,也可以清理浏览器缓存。

答案 2 :(得分:0)

因为你有一个cookie,就像你在每个页面会话中一样,这就是创建的方式。它们被存储起来。如果你想删除,只需转到配置,删除cookie

答案 3 :(得分:0)

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