Routes - URLs on Rails

posted Fri, 07 Oct 2005 17:18:00 GMT by Jonas Bengtsson

As I was getting ready to get this blog out the door I wanted to change the permalinks for the entries. Normally on a Typo powered blog the entries are places under /articles/YYYY/MM/DD/name/, but I wanted to change from articles to archives. So I started updating config/routes.rb and all the places where links were created (which is quite a few). I actually got a little angry that they didn’t use custom helpers to generate all links, since it would be much easier to make the change in on place (putting them together with the routes would be even better, I thought, to honor DRY).

But somehow I realised that Rails was cleverer than that. Actually, routing works both ways – both for incoming requests and for URL generation. url_for (which is used to create URLs for links etc.) uses the routing to create the correct URLs. So to change from articles to archives all that was needed was to change config/routes.rb and everything worked. I think it’s neat with separation between URL design and controller names!

Rails’ “convention over configuration” hides some of its neatness so you’ll have to do some digging.

Comments Zero comments

Comments

Respond

This is where you come in. Please leave a comment whether you agree, disagree or none of the above.

(required)

(to your blog or homepage)

(won't be shown nor shared)


  Preview