2009-12-01 137 views
0

默認情況下,當我問軌控制器做消息/指數,他確實軌道 - 如何覆蓋默認視圖

def index 
respond_to{|fmt| fmt.html} 
end 

和顯示應用/視圖/消息/ index.html.erb

有一個客戶希望他的平臺實例以不同方式顯示意見(並且只能使用css進行更改)。

解決方案,我認爲將是

  1. 創建目錄應用程序/視圖/#{客戶名稱},這將具有相同結構的應用程序/意見,但只會有哪些必須覆蓋默認的視圖。

  2. 在客戶特定的配置文件

  3. 設置含有的其必須被重寫(如果不是,就應該加載默認視圖)視圖列表數組常量

    CUSTOM_VIEWS["messages"]=["index","show","edit"] 
    

    某處所有的控制器動作做類似

    def index 
    respond_to do |fmt| 
        fmt.html do 
        if CUSTOM_VIEWS[params[:controller]].include?(params[:action]) 
        #override default app/views/messages/index.html.erb with app/views/customername/messages/index.html.erb 
        render "#{customername}/#{params[:controller]}/#{params[:action]}" 
        end 
        end 
    end 
    end 
    

還是有更好/更快的解決方案/插件來做到這一點?

回答

1

我相信 「view_paths」 與 「prepend_view_path」 一起使用可回答我的問題

例如

http://www.axehomeyg.com/2009/06/10/view-path-manipulation-for-rails-with-aop/

UPD:

用簡單的添加到application_controller解決了

def override_views 
if APP_CONFIG['pr_name']!=nil 
     ActionController::Base.view_paths=[RAILS_ROOT+"/app/custom_views/"+APP_CONFIG['pr_name'],RAILS_ROOT+"/app/views/"] 
end 
end 

其中APP_CONFIG ['pr_name']是特定產品名稱。

基本上它加載自定義視圖從應用程序/ custom_views /客戶名稱/如果它存在於特定的控制器動作,如果不是它加載從應用程序/視圖/

+0

這不幸的是跨請求堅持默認視圖。 – aceofspades 2011-11-08 23:30:48