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)

No comments: