2011-11-02 47 views
0

我對我的routes.rb的行爲有疑問。下面的代碼完美地工作,但這意味着GET /noodlesGET /將返回麪條的索引。因爲noodles_path將使用/noodles網址變得不必要的長,它不斷彈出。Rails路線,使用match的資源是什麼?

root :to => 'noodles#index' 
match '/new' => 'noodles#new', :as => :new_noodle, :via => [:get] 
resources :noodles, :only => [:index, :create, :show] 

在試圖移除GET /noodles這個我試過:

root :to => 'noodles#index' 
match '/new' => 'noodles#new', :as => :new_noodle, :via => [:get] 
resources :noodles, :only => [:create, :show] 
match '/' => 'noodles#index', :as => :noodles, :via => [:get] 

這似乎工作,但不知何故,打破了:create方法,因爲他們都有一個noodles路徑,即使我指定它到:get只有它感到困惑(rake routes看起來不同,noodles被提及兩次,而當使用resources (...)它只提到一次)。所以這些新路線不可能創造新的麪條。

刪除GET /noodles索引並將其設置爲GET /而不打破:create的正確方法是什麼?

回答

1

事實上,如果你不真的需要,不要將非機智的路線與足智多謀的路線混合在一起。在這種情況下,你不會。

root :to => 'noodles#index' 
resources :noodles 

這是你想要一個,你將有「/」和「/麪條」的路徑帶領您到麪條#索引,這就是你想給想要什麼。

看看你的耙路線,你正在創造一些醜陋的東西;)如果你決定從麪條資源中曲線索引路線,如resources :noodles, :except => :index,你也正在擺脫noodles_path和noodles_url方法。根源在於,只要您想要,您都可以改變主意。也許它會是sign_up頁面,也許有些不同,也許它不會改變,但你不想失去那種自由。

0

假設最好的辦法是當試圖去索引,並使用這些航線

root :to => 'noodles#index' 
resources :noodles, :except => :index 

所以基本上這只是創建的所有路由,除了:index使用root_url,而不是noodles_url,因爲這條路已經存在爲root。然後在視圖中使用root_url