2012-04-11 62 views
2

我想通過在Rails應用程序中重新打開它們來擴展引擎中的模型和控制器。問題是它們在應用程序啓動時未加載。我知道有一些解決方案,如Rails engines extending functionalityhow to override rails 3 engine models and controllers in the main application?,但我懷疑這是由於軌道的加載順序,應該有一些整潔的解決方案。從Rails 3.x中重新打開模型和控制器應用程序

然後,我遇到這種解決方案:

config.railties_order = [Blog::Engine, :main_app, :all] 

然而,模型和控制器在發動機加載,而不是在軌道的人。 想知道是否有人做過這項工作嗎?

+0

這裏。一個乾淨的解決方案[使用顧慮] [1]。 [1]:http://stackoverflow.com/questions/11675951/testing-model-extensions-for-engines – 2012-07-26 18:43:47

回答

0

您可以通過讓主Rails應用程序控制器從Rails引擎繼承來重新打開控制器類。這並不需要config.railties_order爲了得到控制器工作,

#/app/controllers/answer_sheets_controller.rb 
require YourCustomEngine::Engine.root.join('app', 'controllers', 'your_custom_engine', 'answer_sheets_controller') 

class AnswerSheetsController < YourCustomEngine::AnswerSheetsController 

從某種原因,這種策略是行不通的車型。

+0

這很傷心。但我已經想出了另一種解決方案,請參閱下文。不漂亮但整齊(不知道如何在評論中放置代碼) – 2012-07-12 12:00:16

+0

@TinmanChan - 這是另一種解決方案。 https://groups.google.com/forum/?fromgroups#!topic/rubyonrails-core/jy2dQ0BQhnM – westonplatter 2012-07-13 16:29:01

0

我的解決方案:

# === in engine 
class EngineNameSpace::Blog 
    # logic goes here 
end 

class Blog < EngineNameSpace::Blog 
    # no codes should go here 
end 

# === in app 
# If I need to extend the Blog class, I will code as below instead of reopenning the class 
class Blog < EngineNameSpace::Blog 
    # do something 
end 

解釋:

滑軌塊引擎類加載,如果他們是相同的文件名/路徑作爲主應用程式中的東西,看到http://www.cowboycoded.com/2011/02/06/making-the-case-for-rails-3-engines/