2009-12-11 69 views
0

我想在我想要保存在我的控制器視圖文件夾的子目錄中的「boiler-plate」頁面上使用此技術(http://blog.hasmanythrough.com/2008/4/2/simple-pages)。RoR:查看和控制器子目錄?

我該怎麼做?以下似乎不起作用:返回「模板丟失」錯誤。

錯誤:

Missing template home/New_York_apartments.erb in view path app/views 

/app 
    /controllers 
    home_controller.rb 
    /old_pages 
     home_controller.rb 
    /views 
    /home 
     about.html.haml 
     contact.html.haml 
     index.html.haml 
     /old_pages 
     New_York_apartments.html.haml 

routes.rb

map.namespace :old_pages do 
    map.connect ':page', :controller => 'home', :action => 'show', :page => HomeController::PAGES 
end 
map.home ':page', :controller => 'home', :action => 'show', :page => HomeController::PAGES 

controllers/home_controller.rb

class HomeController < ApplicationController 

    # boiler-plate pages 
    PAGES = ['about','contact'] 

    def index 
    # homepage 
    end 

    def show 
    render :action => params[:page] # passed in our routes 
    end 

end 

controllers/old_pages/home_controller.rb

class OldPages::HomeController < ApplicationController 

    # boiler-plate pages 
    PAGES = [ 
    'New_York_apartments' # apprently something to do with new york apartments; who knows 
    ] 

    def show 
    render :action => params[:page] # passed in our routes 
    end 

end 

回答

0

這是你的問題的根源:

map.namespace :old_pages do 
    map.connect ':page', :controller => 'home', :action => 'show', :page => HomeController::PAGES 
end 

這告訴Rails前綴oldpages/它需要查找任何路徑。控制器將在app/controllers/old_pages/home_controller並回應以/old_pages/home開頭的網址。它期望的意見是在app/views/old_pages/home/。您的目錄佈局放置在app/views/old_pages/home

意見不管你選擇如何選擇解決這個問題,你將要移動文件,並可能編輯其他人。

盡力而爲解決方案:將您當前的app/views/home/old_pages目錄重命名爲app/views/old_pages/home。如果你正在遷移你的整個網站,並希望保持目前的版本爲固執的用戶,這是做到這一點的最佳方式。

0

好的,找到了解決辦法。我交易了一個稍微更臃腫的主home_controller.rb爲清潔控制器目錄和路由文件,考慮到這些網頁最終將被淘汰(子目錄中的),我認爲是公平的妥協。

編輯的文件抄錄如下:


/app 
    /controllers 
    home_controller.rb 
    /views 
    /home 
     about.html.haml 
     contact.html.haml 
     index.html.haml 
     /old_pages 
     New_York_apartments.html.haml 

routes.rb:

# custom routes 
    map.root :controller => 'home', :action => 'index' # root page 
    map.home ':page', :controller => 'home', :action => 'show', :page => HomeController::PAGES # boiler-plate pages 
    map.connect ':page.html', :controller => 'home', :action => 'show_old_pages', :page => HomeController::OLD_PAGES # old static pages; only here to make the transition 

controllers/home_controller.rb:

class HomeController < ApplicationController 

    def index 
    # homepage 
    end 

    def show 
    render :action => params[:page] # passed in our routes 
    end 

    def show_old_pages 
    render :action => "old_pages/#{params[:page]}" 
    end 

    #### boiler-plate pages #### 

    PAGES = [ 
    'about', 
    'contact' 
    ] 

    OLD_PAGES = [ 
    'New_York_apartments' # apprently something to do with new york apartments; who knows 
    ] 

end