问题
Is it possible to skip a before filter in using skip_before_filter
when the before_filter is defined with a block. For example:
class ApplicationController < ActionController::Base
before_filter do |controller|
# filter stuff
end
end
I know, that is it possible using the "normal" way of defining the filter with a method name.
回答1:
Its most definitely not possible. From the documentation:
Note that skipping uses Ruby equality, so it’s impossible to skip a callback defined using an anonymous proc using #skip_filter
http://apidock.com/rails/AbstractController/Callbacks/ClassMethods/skip_action_callback
回答2:
I actually found a way around this using an if option with a method. Use
class ApplicationController < ActionController::Base
before_filter(:if => :use_filter?) do |controller|
# filter stuff
end
private
def use_filter?
true
end
end
class OtherController < ApplicationController
private
def use_filter?
false
end
end
:D
来源:https://stackoverflow.com/questions/19021465/skip-before-filter-defined-with-block