DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Alternative Default Routes

09.27.2005
| 8681 views |
  • submit to reddit
        I prefer :controller/:id/:action formatted URIs to :controller/:action/:id. For instance, "people/56/edit" to "people/edit/56". This is a simple matter of taste and has no real benefits other than being a bit more intuitive to certain brains. 

It's also a bit harder to do than you might expect. You need to replace the default :controller/:action/:id route with the following:

map.connect ':controller/:action', 
            :requirements => { :action => /\D+/ }

map.connect ':controller/:id/:action',
            :action => 'show',
            :requirements => { :id => /\d+/ }

This will result in the following URIs for the basic set of CRUD actions generated by default:

/people/         (:action => 'index')
/people/new
/people/create
/people/56/      (:action => 'show' by default)
/people/56/edit
/people/56/remove
/people/56/update

You could argue that the default route setup in Rails is a correct default because it's a bit more simple to apply across all cases.