2014-01-24 48 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

可以帮助你。