:除了不在应用程序控制器中的before_filter中工作。 路由问题?

我的应用程序控制器中有一个before_filter来保持用户的会话处于活动状态(如果已经超时,则将其注销)。 这应该在除/ sessions / new和/ sessions / destroy之外的每个操作上调用,这些操作被路由为/ login和/ logout。

我的应用程序控制器的相关部分看起来像这样;

class ApplicationController  [:login, :logout] private def update_activity_time if current_user time_out = current_user.setting.remember_me ? 20160 : current_user.setting.user_timeout from_now = time_out.minutes.from_now end if session[:expires_at].blank? session[:expires_at] = from_now else time_left = (session[:expires_at].utc - Time.now.utc).to_i if time_left  "window.location.replace(\"#{login_url}\")" end end end 

和我的routes.rb包含以下内容;

 map.login "login", :controller => "sessions", :action => "new" map.logout "logout", :controller => "sessions", :action => "destroy" 

正在访问/ login或/ logout时调用before_filter。 这不是一个显示停止,但它确实会导致一些奇怪的行为(例如,当从已超时的页面退出时)。

我有什么想法我做错了吗? 我正在使用Rails 2.3.10。

:except选项采用动作名称,而不是url部分。 这是你应该做的事情:

 class ApplicationController < ActionController::Base before_filter :update_activity_time ... end 

然后,在sessions_controller.rb

 class SessionsController < ApplicationController skip_before_filter :update_activity_time, :only => [:new, :destroy] ... end 

您不希望在ApplicationController放置:except ,因为如果您这样做,每个应用程序控制器的newdestroy操作都不会更新活动时间。