Recursive routes in Rails

青春壹個敷衍的年華 提交于 2019-12-03 09:25:15

问题


Is is possible to create a recursive route in Rails?

I have an application, which allows a admin to create pages. The page model is a nested set and so each page has a parent_id hence the pages are structured in trees. The page model also uses the Friendly ID plugin to provide slugs for each page.

When a user browses the site I would like them to see the nesting structure in the urls - its better for search engine purposes as well as any users who might like to browse the site by chopping the urls.

I want something along the lines of:

http://example.com/page/page/page/page ...etc

Now obviously I can create a nested map with say 10 nests and hope that no site exceeds that limit, but I'm curious if there is another way...


回答1:


You can map the initial route (/page) to the controller, setting up "globbing" for all the trailing parameters.

map.connect '/:page/*pages', :controller => 'pages', :action => 'show' 

params[:pages] will now contain an array of the page parameters (matching as many trailing params as you specify in the URL).



来源:https://stackoverflow.com/questions/2353134/recursive-routes-in-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!