在Rails中跳过before_filter
为了清楚起见,名称和对象已被简化。 基本概念保持不变。
我有三个控制器: dog , cat和horse 。 这些控制器都从控制器animalinheritance。 在控制器animal ,我有一个用于validation用户的filter: 
 before_filter :authenticate def authenticate authenticate_or_request_with_http_basic do |name, password| name == "foo" && password == "bar" end end 
 在dog的show行动中,我需要对所有用户开放访问(跳过authentication)。 
 如果我要为dog单独编写validation,我可以这样做: 
 before_filter :authenticate, :except => :show 
 但是,由于dog从animalinheritance的,我没有访问控制器的具体行为。 在animal控制器中join:except => :show不仅可以跳过对dog的show动作的authentication,还可以跳过cat和horse的show动作。 这种行为是不希望的。 
 在inheritanceanimal同时,我怎样才能跳过只用于dog的show动作的authentication? 
 class Dog < Animal skip_before_filter :authenticate, :only => :show end 
有关filter和inheritance的更多信息,请参阅ActionController :: Filters :: ClassMethods 。
给出的两个答案是一半的权利。 为了避免让所有的狗动作都打开,您需要限定skip_before_filter,以便仅应用于“show”操作,如下所示:
 class Dog < Animal skip_before_filter :authenticate, :only => :show end 
为此,您可以使用skip_before_filter
这在Rails API中有解释
 在你的例子中, dog需要包含 
 skip_before_filter :authenticate 
 只是一个使用rails 4的小更新,现在是skip_before_action :authenticate, :only => :show ,而before_filters现在应该使用before_action 。