Tutorial :Rails 2 Trailing Route Parameter


I'm building an article/blog website where any article may show up within a series of URL paths.

/section/article  /section/page/article  /section/page/page2/article  

This works, but what if I wanted to have the page number for that article (/page/123) be bound to those URLs

/section/article/page/123/  /section/page/article/page/123  /section/page/page2/article/page/123  

This would mean that I would have to create a specific route for each different url?


This would mean that I would create dozens of URL routing paramters.

Is there anyway in rails to say that all urls may have a /page/NUMBER suffix at the end of the URL and still route normally (that is assign the NUMBER to a parameter and continue to goto the page normally)?


Route globbing, described at http://guides.rubyonrails.org/routing.html#route-globbing, might work in this situation. For example, your route might read map.connect '/:section/*page_subpage_path/page/:number', :controller => 'articles', :action => 'show'

This exact code might not work as intended, but this method might be a good direction to try. Good luck :)


If you want to create routes that are as customized as that you normally need to create a large number of routes to accommodate them. The general format is /resource/:id when using map.resource, anything other than that is left to you to specify.

Given that the Rails routes.rb file is executable ruby you can often define your routes programmatically by repeating patterns or doing combinations on arrays if required.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »