2010-12-09 53 views
0

我有兩個具有has-many/belongs-to關係的模型。該航線是嵌套的,我有這個在我的routes.rb:自定義Rails路徑以刪除模型名稱

resources :threads do 
    resources :posts 
end 

所以我得到這樣example.org/threads/147/posts/372 URL和example.org/threads/298等。

我該如何改變,以便URL更像example.org/147/372,模型隱含?

回答

1

你可以配置這樣的路線:

match ':id' => 'threads#show' 
match ':thread_id/:id' => 'posts#show' 

這會工作,但它也將導致其他路線問題,因爲它不僅會匹配example.org/123/456,它還會匹配example.org/user/mark

,以確保它僅匹配可能的ID(數字)的模型,您可以添加約束這樣的:

match ':id' => 'threads#show', :constraints => { :id => /\d*/ } 
相關問題