我想向用户显示超时警告(某种倒计时表示会话超时前2分钟)
我不知道如何显示警告before
超时
我目前在ApplicationController
类中使用此代码来检查每次控制器被命中时用户是否登录/有效:
def check_login
logger.info "ApplicationController check_login"
if session[:current_user]
<do something>
else
redirect_to login_path, flash: {notice: 'We require you to sign in.'}
end
end
但我不知道如何显示警告2分钟before
会话超时。请帮忙!
你可以在application_controller.rb中使用before_action过滤器
Class ApplicationController
before_action :check_session_has_expired
def check_session_has_expired
if !session[:user].present?
redirect_to :session_warning_url
end
end
end
好的,你也可以在会话中设置expire_at
session[:expire_at] = Time.current + 24.hours
然后,每两分钟对服务器发出一次ajax请求
并检查以下条件
if Time.current - session[:expires_at] < 2.minutes
# show warning
end