我正在使用 ruby 1.9.3 和 rails 3.2。
我的实际会话处理如下所示:
会话助手
def sign_in(user)
cookies[:remember_token] = {value: user.remember_token, expires: 20.minutes.from_now.utc}
self.current_user = user
end
def sign_out
self.current_user = nil
cookies.delete(:remember_token)
end
def signed_in?
if current_user.nil?
return false
else
cookies[:remember_token] = {value: current_user.remember_token, expires: 20.minutes.from_now.utc}
end
def current_user=(user)
@current_user = user
end
def current_user
@current_user ||= User.find_by_remember_token(cookies[:remember_token])
end
应用控制器
class ApplicationController < ActionController::Base
before_filter :check_signin
protect_from_forgery
include SessionsHelper
def check_signin
unless signed_in?
redirect_to signin_path
end
end
end
如果会话已过期,我想通知用户@登录页面。如何检查会话是否过期?请给出一个代码示例。