使用 before_filter,是否可以排除已安装引擎的操作?

PJC*_*PJC 1 ruby-on-rails ruby-on-rails-3

在我的 Rails 应用程序中,我正在安装一个外部引擎。我有一个before_filter在我ApplicationController,我需要排除一些从该过滤器发动机的行动。

通常,我会skip_before_filter在相应的控制器中使用,但我宁愿不接触引擎代码本身,因为它不是我的。

有没有办法做到这一点?

class ApplicationController < ActionController::Base

  before_filter :authorize, :except => [:engine/setup] # something like this?
  ...
Run Code Online (Sandbox Code Playgroud)

谢谢,

PJ

小智 5

只是为了添加orien的答案,您需要在引擎中指定特定的控制器,或者只是ApplicationController

EngineController::ApplicationController.class_eval do
  skip_before_filter :authorize, :only => [:setup]
end
Run Code Online (Sandbox Code Playgroud)

此外,如果您希望在开发模式下对每个请求重新加载过滤器跳过:

Rails.application.config.to_prepare do
  EngineController::ApplicationController.class_eval do
    skip_before_filter :authorize, :only => [:setup]
  end
end
Run Code Online (Sandbox Code Playgroud)