我想向用户显示超时警告(某种倒计时,例如会话超时前 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