Skip before_filter defined with block

雨燕双飞 提交于 2019-12-24 01:45:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!