如何防止在特定页面上自动注销用户?

问题描述:

我在我的RoR站点上设计了基于设计的验证系统,我需要在闲置一段时间后自动注销用户。但是我的网站上也有一些页面,这些页面长时间打开(用户只会看页面,信息由ajax更新),并且我不想在打开此页面时退出用户。如何防止在特定页面上自动注销用户?

有没有人有一个想法如何做到这一点?或者如何告诉Devise ajax请求也是用户活动?

首先确保你已经安装了1.5.2的设备,如果没有,升级它,在这个问题上已经有6个月了:-)我希望你已经解决了这个问题。

当您位于要阻止自动注销的页面时,您可以更改timeout_in返回值。

例如:

class Student < ActiveRecord::Base 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable, :timeoutable 

    def timeout_in 
     if session[:auto_sign_out] then 
     #the normal period of signout 
     30.minutes 
     else 
     #very long period, to prevent sign-out 
     100.years 
     end 
    end 
end 

然后,可以很容易地在ApplicationController中添加的before_filter设置session[:auto_sign_out]

这样的:

before_filter :manage_page_auto_sign_out 


def manage_page_auto_sign_out 
# check the params, and see if the id is the id of the specific page 
if params[:id] == EXCLUDED_PAGE_ID then 
session[:auto_sign_out]= false 
else 
session[:auto_sign_out]= true 
end 
end 

另外,可以添加其他条件通过检查页面的控制器名称来确保您正在检查您想要的页面:

def manage_page_auto_sign_out 
# check the params, and see if the id is the id of the specific page 
if controller_name == 'pages' && params[:id] == EXCLUDED_PAGE_ID then 
session[:auto_sign_out]= false 
else 
session[:auto_sign_out]= true 
end 
end 

你需要检查的页面控制器的名字,我希望这有助于

+0

嗯,这并不因为你的工作无法访问会话模型类(学生): 未定义的局部变量或方法'会话' – 2012-01-19 13:06:52