【问题标题】:Skip before_filter defined with block跳过使用块定义的 before_filter
【发布时间】:2013-09-26 07:00:19
【问题描述】:

当使用块定义 before_filter 时,是否可以在使用 skip_before_filter 时跳过前过滤器。例如:

class ApplicationController < ActionController::Base
  before_filter do |controller|
    # filter stuff
  end
end

我知道,可以使用“正常”的方式使用方法名称定义过滤器。

【问题讨论】:

  • 不这么认为,把它移到一个方法中

标签: ruby-on-rails ruby-on-rails-3.2 before-filter


【解决方案1】:

我实际上找到了一种方法,使用带有方法的 if 选项。使用

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

【讨论】:

  • 或者你可以使用 :unless => inherit_controller? def 继承控制器? self.class != ApplicationController 结束
【解决方案2】:

绝对不可能。来自文档:

请注意,跳过使用 Ruby 相等性,因此无法使用 #skip_filter 跳过使用匿名 proc 定义的回调

http://apidock.com/rails/AbstractController/Callbacks/ClassMethods/skip_action_callback

【讨论】:

    猜你喜欢
    • 2011-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-05
    • 1970-01-01
    相关资源
    最近更新 更多