Routing is one of those aspects of Rails which tends to get neglected. By just making a few small changed, you can significantly contribute to your SEO effort and to the readabilty/navigability of your web app.

Pretty URLs For Date Related Data

If you have a blog, like I do, you may want to make your post archives available via easy to understand URLs. To do this you can create a new route in you routes.rb file as follows:-

http://gist.github.com/200058.js">

So, lets step through this. First of all we map the path 'archives/:year/:month' - this specifies the format of the URL that this route will expect. Next, we state which controller and action will be called, in this case that will be the "posts" controller and the "find_by_date" action/method. Further to this, we can specify what pattern ":year" and ":month" should match with regular expressions. ":year" will match a four digit number and ":month" will match a month name in the example above. I have included some examples of the URLs this route will match:-

<a href="http://localhost:3000/archives/2007/mar">http://localhost:3000/archives/2007/mar</a>
<a href="http://localhost:3000/archives/2008/apr">http://localhost:3000/archives/2008/apr</a>
<a href="http://localhost:3000/archives/2009/may">http://localhost:3000/archives/2009/may</a>

You can then use the parameters ":year" and ":month" in your controller to retrieve the relevant data with a method similar to below:-

http://gist.github.com/200062.js">

Pretty URLs For Your Blog Posts

This is really simple. You can simply place the following method in your "Post" model (or whatever your model is called). Just remember to change the "title" field with whatever your post title field is.

http://gist.github.com/200064.js">

Here, we are basically overriding the to_param method, which is used in ActiveRecord to return the its primary key. For more on this pop over to Obie Fernandez's blog.