2014-01-24 50 views
0

說我有一個標準的Rails應用程序有五種型號:Topic,Post,Author,CommentCommentAuthor。我想要Posts可用,如domain.com/:author_name/32,我想要Topics可用,如domain.com/12Rails:複雜的路由

這就像我希望一個模型在根目錄下可用,而不會干擾其他人的自然層次結構。這在Rails中甚至可能嗎?

UPDATE

這是因爲我一直喜歡這個收到錯誤加載資源時:

{"controller"=>"topic", "action"=>"show", "post"=>"assets", "id"=>"social-icons", "format"=>"css"} 
+3

是的! http://guides.rubyonrails.org/routing.html –

回答

2

爲主題,假設你的控制器主題控制器

get '/:id', to: 'topics#show' 

有關的職位,這將是

get '/:author_name/:id', to: 'posts#show' 

另外,請必須通過導遊讀:http://guides.rubyonrails.org/routing.html

+0

是author_name不是一個參數? – emaillenin

+0

是的,我的意思是'author_name'作爲參數。 –

+0

對不起,讓我編輯它。 –

0

你可能會想一個嵌套的路線,與

resources :authors do 
    resources :posts 
end 

假設你做你的模型關聯正確,這將是您的路由的開始。要獲取domain.com/author_name/32,您需要自定義一下。也許

https://github.com/norman/friendly_id

可以幫助你。