Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursive routes in Rails

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...

like image 778
ktec Avatar asked Feb 28 '10 23:02

ktec


1 Answers

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).

like image 108
Toby Hede Avatar answered Nov 15 '22 13:11

Toby Hede