问题
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