让Rails也拥有JavaEE中的Session过期的Listener功能

在controllers/application.rb中加如下的过滤器代码,优雅又实用。

before_filter :session_expiry, :except => [:login, :logout]
before_filter :update_activity_time, :except => [:login, :logout]

def session_expiry
  @time_left = (session[:expires_at] - Time.now).to_i
  unless @time_left > 0
    reset_session
    flash[:error] = 'Sua sessão expirou. Favor entrar novamente.'
    redirect_to :controller => 'login', :action => 'login'
  end
end

def update_activity_time
  session[:expires_at] = 60.minutes.from_now
end



同时要在login的控制器中调用
update_activity_time

你可能感兴趣的:(javaee,Flash,Ruby,Rails)