Ruby on rails 如果用户忘记注销并关闭浏览器,如何删除Rails中以前的会话?

Ruby on rails 如果用户忘记注销并关闭浏览器,如何删除Rails中以前的会话?,ruby-on-rails,session,logout,Ruby On Rails,Session,Logout,我将所有会话信息存储在数据库中。用户可以登录系统进行操作。有一些限制区域要求用户登录。如果用户试图在登录前到达这些区域,系统会将其重定向到登录页面 有时用户可能会在不注销的情况下关闭浏览器。如果发生这种情况,下次他尝试登录时,系统无法识别新生成的会话,即使用户输入了正确的用户名和密码。然后,系统将强制用户再次登录 有人知道怎么解决这个问题吗?在加载登录页面之前,我尝试使用reset\u session重置会话。这个问题无法解决 会话控制器 def new end def creat

我将所有会话信息存储在数据库中。用户可以登录系统进行操作。有一些限制区域要求用户登录。如果用户试图在登录前到达这些区域,系统会将其重定向到登录页面

有时用户可能会在不注销的情况下关闭浏览器。如果发生这种情况,下次他尝试登录时,系统无法识别新生成的会话,即使用户输入了正确的用户名和密码。然后,系统将强制用户再次登录

有人知道怎么解决这个问题吗?在加载登录页面之前,我尝试使用
reset\u session
重置会话。这个问题无法解决

会话控制器

  def new
  end

  def create
    session[:current_account] = Account.authenticate(params[:email], params[:password])
    if session[:current_account]
      redirect_to :controller => "xxxxx", :action => "index"
    else
      flash[:notice] = "Please try again."
      render :action => 'new'
    end
  end
  before_filter :permission_handling

  def index
  end
  def permission_handling
    unless logged_in?
      forced_login
    end
  end

  def logged_in?
    session[:current_account].is_a?(Account)
  end

  def forced_login
    flash[:notice] = "You haven't login yet!"
    redirect_to ( :controller => "sessions", :action => "new" )
  end
账户模式

  def self.authenticate(email, pass)
    account = find_by_email(email)
    account && account.authenticated?(pass) ? account : nil
  end

  def authenticated?(pass)
    encrypted_password == Account.encrypt(pass,salt)
  end
Xxxxx控制器

  def new
  end

  def create
    session[:current_account] = Account.authenticate(params[:email], params[:password])
    if session[:current_account]
      redirect_to :controller => "xxxxx", :action => "index"
    else
      flash[:notice] = "Please try again."
      render :action => 'new'
    end
  end
  before_filter :permission_handling

  def index
  end
  def permission_handling
    unless logged_in?
      forced_login
    end
  end

  def logged_in?
    session[:current_account].is_a?(Account)
  end

  def forced_login
    flash[:notice] = "You haven't login yet!"
    redirect_to ( :controller => "sessions", :action => "new" )
  end
应用程序控制器

  def new
  end

  def create
    session[:current_account] = Account.authenticate(params[:email], params[:password])
    if session[:current_account]
      redirect_to :controller => "xxxxx", :action => "index"
    else
      flash[:notice] = "Please try again."
      render :action => 'new'
    end
  end
  before_filter :permission_handling

  def index
  end
  def permission_handling
    unless logged_in?
      forced_login
    end
  end

  def logged_in?
    session[:current_account].is_a?(Account)
  end

  def forced_login
    flash[:notice] = "You haven't login yet!"
    redirect_to ( :controller => "sessions", :action => "new" )
  end

谢谢大家。:)

看起来您正在会话中存储帐户对象。不建议这样做:您应该只存储简单的值,比如会话中的帐户id

更改
模型。验证
以返回
帐户.id
,并通过从数据库加载帐户而不是将其保留在会话中来检查
已登录


我不确定这是否完全解决了这个问题,但这可能很重要。

谢谢,伙计。我试试看