2011-11-02 41 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