2013-07-28 33 views
2

在Rails 4,具有與安裝的發動機上的應用程序時,有兩個ApplicationController軌道4,過濾器應用到安裝的發動機的行動

  • class ApplicationController < ActionController::Base的應用
  • class MyEngine::ApplicationController < ActionController::Base發動機

由於引擎ApplicationController未從應用程序ApplicationController繼承,因此應用於應用程序的過濾器不會應用於引擎。

如何將before_action,after_actionaround_action應用於所有應用路徑,包括已安裝引擎的路徑?當然,不用接觸引擎代碼。

非常感謝。

回答

3

您可以創建一個改變的ActionController :: Base的

class ActionController::Base 
    before_action :do_a_thing 
    def do_a_thing 
    puts "did something" #or whatever 
    end 
end 

棒這在RB文件的初始值,並把它在配置/初始化

+0

謝謝!那工作 –

+0

謝謝!完善。 – jvillian

0

您可以在engine.rb文件中添加過濾器您發動機

initializer :append_before_action do 
    ActionController::Base.send :before_action, :do_a_thing 
end