2013-10-16 38 views
6

我已閱讀the Rails Guides將定製路線添加到Rails應用程序

我想成立是被路由到「配置文件」控制器以下路線:

GET profiles/charities - 如果顯示所有慈善機構
GET profiles/charties/:id應顯示specfic慈善
GET profiles/donors - 應該顯示所有捐助者
GET profiles/donors/:id - 應顯示特定捐贈者

我已創建配置文件控制器和兩種方法:慈善機構和捐助者。

這就是我需要的嗎?

+0

我認爲你做錯了,那些是「子資源」,你需要創建一個慈善機構和捐助者控制器,並執行一個路線'資源:慈善機構,只有:[:index,:show]'或類似的東西您的路線文件,在配置文件資源中 –

回答

12

下面將設立路線,你想要什麼,但將它們映射到:indexCharitiesController:showDonorsController

namespace :profiles do 
    # Actions: charities#index and charities#show 
    resources :charities, :only => [:index, :show] 

    # Actions: donors#index and donors#show 
    resources :donors, :only => [:index, :show] 
end 

當它是更合適的設置自定義路線,這樣的事情會做:

get 'profiles/charities', :to => 'profiles#charities_index' 
get 'profiles/charities/:id', :to => 'profiles#charities_show' 
get 'profiles/donors', :to => 'profiles#donor_index' 
get 'profiles/donors/:id', :to => 'profiles#donor_show' 

下面是指南中的相關章節,你是經歷:

  1. Resource Routing: the Rails Default - Controller Namespaces and Routing
  2. Non-Resourceful Routes - Naming Routes
2

的慈善機構和捐助者似乎是嵌套的資源。如果是這樣,在你的config/routes.rb中的文件,你應該有這樣的事情,

resources :profiles do 
    resources :charities 
    resources :donors 
end 

因爲這些嵌套資源,你不需要這兩種方法命名的慈善機構和捐助者在您的配置文件控制器。事實上,根據您的應用程序,您可能需要單獨的控制器和/或模型爲您的慈善機構和捐助者。

相關問題