Routes - URLs on Rails
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.
Zero comments