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

Ruby on rails Rails教程(M.Hartl)第三版,第8章,为什么应用程序无法忘记登录状态?,ruby-on-rails,ruby,railstutorial.org,Ruby On Rails,Ruby,Railstutorial.org,在登录到应用程序,然后完全关闭浏览器后,我重新打开浏览器,希望注销。但是我仍然登录 我在第节中,除上述内容外,应用程序的外观和运行与预期一致。此外,代码与教程中的代码相同。我认为这些是相关文件: app/helpers/sessions\u helper.rb module SessionsHelper # Logs in the given user. def log_in(user) session[:user_id] = user.id end # Return

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

我在第节中,除上述内容外,应用程序的外观和运行与预期一致。此外,代码与教程中的代码相同。我认为这些是相关文件:

app/helpers/sessions\u 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
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
app/controllers/sessions\u controller.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
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
class sessioncontroller
在登录到应用程序,然后完全关闭浏览器后,我重新打开浏览器,希望注销。但是我仍然登录

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


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

因为您有一个cookie,这是您在每个页面会话中喜欢的方式,这就是创建cookie的方式。它们被储存起来。如果您想删除,只需转到配置,删除cookie即可。通常,关闭浏览器时,
会话将被清除,因此在这种情况下,您应该自动注销。不过,正如中所讨论的,有些浏览器会记住您的会话,这可能就是这里发生的事情。

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

该应用程序(迄今为止)设计为在关闭浏览器时自动注销,这是预期的行为。您是否查阅第8.3节?在该部分中,教程将解释如何添加注销方法。如果没有注销方法,您的浏览器会缓存会话。请参阅第8.2.3节的结尾,它会说:“如果您完全退出浏览器,您还应该能够验证应用程序是否忘记了您的登录状态”@Lorenz我想您不理解这里的问题。在该应用程序中,一旦关闭浏览器,用户将自动注销,但其行为并非如此。您完全可以单击“注销”按钮并执行此操作,但问题是关于允许用户在关闭浏览器后注销的功能。这很有帮助。我想我的Chrome OS浏览器可能会有问题,因为即使在更改了“继续你离开的地方”的设置之后,我也会遇到这个问题。但是,在Windows上使用常规的Chrome浏览器时,此修复程序可以正常工作。IE11也很好。至少我知道我没有错误地复制你的代码。