2012-01-09 22 views
2

任何人都可以通過正確的方式引導我到將現有的幫助器添加到擴展控制器之前不包含此幫助器。在擴展控制器(Redmine Plugin Dev)中添加現有的幫助

例如,我已經在timelog_controller_patch.rb延長timelog_controller.rb控制器。於是,我試圖添加助手查詢,這帶來了一定的功能,我想在我的補丁使用。

如果我在補丁添加輔助(我timelog擴展控制),我總是得到同樣的錯誤:

錯誤:未初始化的常量的Rails插件:: :: TimelogControllerPatch(NameError)

下面是我已經如何操作的實例:

module TimelogControllerPatch  
    def self.included(base) 
     base.send(:include, InstanceMethods) 
     base.class_eval do 
      alias_method_chain :index, :filters 
     end 
    end 
    module InstanceMethods 
     # Here, I include helper like this (I've noticed how the other controllers do it) 
     helper :queries 
     include QueriesHelper 

     def index_with_filters 
      # ... 
      # do stuff 
      # ... 
     end 
    end # module 
end # module patch 

然而,當我包括原始控制器相同的助手,一切正常(當然,這是不正確的做法)。

有人能告訴我什麼我做錯了嗎?

感謝提前:)

回答

4

helper方法需要被調用的控制器類,是放入它是沒有得到正確運行的模塊。這將工作:

module TimelogControllerPatch  
    def self.included(base) 
     base.send(:include, InstanceMethods) 
     base.class_eval do 
      alias_method_chain :index, :filters 
      # 
      # Anything you type in here is just like typing directly in the core 
      # source files and will be run when the controller class is loaded. 
      # 
      helper :queries 
      include QueriesHelper 

     end 
    end 
    module InstanceMethods 
     def index_with_filters 
      # ... 
      # do stuff 
      # ... 
     end 
    end # module 
end # module patch 

隨意看任何我在Github上的插件,我的大部分補丁將在lib/plugin_name/patches。我知道我有一個助手,但現在找不到它。 https://github.com/edavis10

附:不要忘記也需要你的補丁。如果它不在您插件的lib目錄中,請使用相對路徑。

埃裏克·戴維斯

+0

謝謝!它像一個魅力!不幸的是,文檔非常稀少,我無法找到解決這個問題的好辦法。非常感謝你。 – 2012-01-17 09:56:25

0

或者,如果你不想使用補丁做到這一點:

Rails.configuration.to_prepare do 
    TimelogController.send(:helper, :queries) 
end