2013-03-15 40 views
1

我正在開發一個基本的rails應用程序,並且想知道如何顯示/users/:id/posts/:id as /posts/:id。現在我的路線是Rails路由:如何顯示/ users /:id/posts /:id as/posts /:id

resources :users do 
    resources :post 
end 

我需要讓每個帖子都得到用戶關聯。但同時爲便於使用,我需要能夠在/posts/:id

顯示帖子讚賞的任何幫助。

感謝

回答

2

可以聲明相同的控制器第二條途徑:

resources :users do 
    resources :posts 
end 

resources :posts 

你只是需要處理您的郵件控制器中沒有user_id參數的優雅。

+2

我會主張在第二個添加'except:[:show]',並且'only:[:index,:show]'。 – thomasfedb 2013-03-15 16:57:40

+0

真棒,而不是索引,我使用主。但是,它做到了。謝謝@thomasfedb :) – psharma 2013-03-16 05:44:04

+0

@psharam'index'會被認爲比'main'更傳統。 – thomasfedb 2013-03-23 19:16:07

2

你可能希望在Rails的選項

resources :users, :shallow => true do 
    resources :post 
end 

http://rails-bestpractices.com/posts/11-needless-deep-nesting

+0

nope淺不幫助。路線仍然是一樣的。 – psharma 2013-03-15 10:02:06

+0

它實際上爲我工作,你的數據庫結構是什麼?並請張貼您的耙路線結果 – 2013-03-15 10:08:20