Thursday, January 25, 2007

Static Controller Shortcuts

Update: Gabe da Silveira is doing something similar at Routing Nested Static Content in Rails.

The first tidbit.

Most sites tend to have at least 4+ static pages. Typically: about us, user agreement, contact, privacy policy and 404. I'll assume you already have a static controller that holds these view templates. Instead of making a bunch of static routes from "/about" to "views/static/about", you can add one simple to direct everything that doesn't go to an existing controller to your static controller.

# Add this after your default route ':controller/:action/:id'
map.static ':action/', :controller=>'static'

Since the Rails default route will only match a url if the controller name exists, /about will not match and it will be displayed by our new static route. For bonus points this is a named route and you can use this code to generate the url for the about page:

static_url(:action => :about)

'Bout time.

Today I decided to resurrect my blogger account from my Informatics days at IU Bloomington, and actually do something worthwhile with it. So here is the story that starts this saga.

Last year I was contracted to code a pretty basic social networking site in Ruby on Rails. Although my main competency was PHP, the client was certain that they wanted it done with Rails. This was fine with me, I had read a few articles about Rails and knew that it had a lot of features that I've been dying for in PHP.

So over the next few months I learned Ruby and Rails and implemented a few iterations of the site. Since Rails is a relative newcomer, I was purchasing lots of books on it and Ruby while subscribing to many ruby news/blog feeds. All the while I struggled through all the deployment issues, multiple hosts and rails version change breaks. If it weren't for the sheer joy of coding Ruby and using Rails, I am sure I would have stopped after a month.

Anyhow, I've come to find that the Rails documentation does go a long way, but I learned the most from reading the weblogs of people who use Rails daily. There is a project underway to improve not only the documentation, but the way the documentation is created. I will happily contribute to that once it is available, but until then I will hopefully be sharing bits of Ruby and Rails wisdom on this blog like those before me.