Using routes.rb to help you structure your Ruby on Rails app
Routes.rb is a really powerful and one the most impressive features of the Rails framework once you work out how to use it.
Flat pages
Rather than using a controller for each flat page on a website you can put them all in the same controller and use routes.rb to ensure your app knows which controller and action to use.
Typically I create a controller called 'base' and use it for all my flat pages. To achieve this I use:
map.connect ':action', :controller => "base"
So now http://www.domain.com/about will go to the 'base' controller and look for the action 'about'.
Using this method it's important to understand that routes.rb has an order of priority, where rules at the top of the file will rule over those below.
So to create a sub section in the site, that has its' own controller, I would do this:
map.news_article '/news/article/:id', :controller => 'news', :action => 'article'
map.connect ':action', :controller => "base"
Now the url will be http://www.domain.com/news/article/1 to go to the article action in the news controller passing the id of 1. However, you can build on this further using named routes.
Named Routes
If you look at the code example above, for the news section i've used map.news_article instead of map.connect. This creates a method called news_article_url. It means you can use:
<%= link_to 'Read more...', news_article_url(:id => @article.id) %>
Which generates the html:
<a href="http://www.domain.com/news/article/1">Read more...</a>
If you use this throughout your site then it means that should you ever rename your controller, then you wont have to go to each link in the site and alter it to reflect the change in name of that controller.
To take this even further you could hide the id passing in the url and create what is known as a permalink. Typically for a news article you would use the title of the article and convert spaces to '-'. e.g. http://www.domain.com/news/article/title-like-this Great for search engine optimisation and far more beautiful. Fortunately it is really easy to do and I shall write an article on it very soon.
Update 17/07/06: If you are interested in permalinking; I found this article entitled 'How to enable permalinking in 3 steps' a great starting point.
Currently I'm undecided on permalinking and there's a good debate about it in this article 'Flexible urls'. The problem being a permalink can lead to a dead link should it be based on data that can ultimately be changed.
Update 20/04/07: Chris Farms has created a plugin called 'Acts as friendly param' that solves all the potential problems with permalinking.
About
Paul is a Rails developer for Kyan web agency.
Checkout his ramblings about music, what he's listening to and his other random thoughts.
He also writes on the Kyan blog.
More snippets
Take a look in the archive
Need a website?
Your best bet is to contact Paul's employer. Make sure to check out the portfolio of work.
Hosting
I recommend hostingrails.com
Recommended Reading
Contact
my name at gmail.com
2 comments made
Nice article, i’m new to rails and i’m sure i’ll need this one day. Thanks again.
great, it worked exactly as you wrote! 10x
Got something to say?