2011-05-19 222 views
10

谁能告诉我如何过滤器之前跳过所有在轨道3轨道3跳过所有过滤器

在轨2.x中,我们可以做

skip_filter filter_chain 

然而filter_chain在轨不再支持3.

感谢

回答

3

还没有尝试过,但是这可能工作:

[:before, :after, :around].each {|type| reset_callbacks(type)} 
+1

'reset_callbacks'将完全删除整个控制器的回调,并且不允许使用':except'或':only'关键字。如果这是可以接受的,它比我的代码更好,更向前兼容。 – Laas 2011-05-19 09:24:11

0

这肯定是里面的Rails腹部深黑客,你会过得更好用手工指定的回调,但是下面一行将做的工作:

eval %[skip_filter #{_process_action_callbacks.select { |c| 
    [:before, :after, :around].include? c.kind }.collect{|c| 
     c.filter.inspect}.join(", ") 
    }] 

您还可以添加:only => :index等修改的EVAL就在结束]之前,在需要时进一步修改呼叫。

18

试试这个

skip_filter *_process_action_callbacks.map(&:filter) 

方法_process_action_callbacks应该返回CallbackChain情况下,这是一个数组Callback小号 而且,由于回调#过滤得到回调的名字,这个工作:

before_filter :setup 
_process_action_callbacks.map(&:filter) #=> [:setup] 
+6

需要为此参数提供参数:'skip_filter(* _ process_action_callbacks.map(&:filter)) – 2011-07-13 01:30:22

+0

修复了基于@ XavierShay的评论的代码 – 2013-11-05 21:23:16

2

如果要指定:仅限或:除skip_filter外,请使用以下内容:

skip_filter(:only => [:method1, :method2], *_process_action_callbacks.map(&:filter)) 

Xavier Shay把我放在了正确的方向,但后来我挣扎了一下,弄清楚了我必须说的:只有在要跳过的过滤器列表之前!

编辑:上面的解决方案是针对RUby 1.8.7。对于Ruby 1.9的你会怎么做:

skip_filter(*_process_action_callbacks.map(&:filter), :only => [:method1, :method2]) 
7

需要跳过所有过滤器可能是由从定制的ApplicationController继承引起的。

class ApplicationController < ActionController::Base 
    # defines multiple `before_filter`s common to most controllers 

class SomeController < ApplicationController 
    # this controller may be fine with inheriting all filters 

class AnotherController < ApplicationController 
    # but this one not! 

在我的示例场景,而不是去除AnotherControllerbefore_filter S,就让它从ActionController::Base继承。