Sunday, 16 September 2007

Creating Static Home Pages in Rails

One trick I've not seen anywhere in my Google searches is to have Rails generate your static html pages for you.

However if you find that you have to build wrapper pages around the front of your application, then you can do a lot worse than use the tools of ERB, et al to reduce the duplication.

What I've used is a HomeController that looks like this:
# Static page generator
class HomeController < ApplicationController
caches_page :index, :donation

end

with a couple of routes at the bottom of routes.rb
  map.home '', :controller => 'home', :action => 'index'
map.connect '/:action/:id', :controller => 'home'

Now you merely create the views you require in views/home and add the name to the caches_page command.

Rails will then generate the static pages on first access for anything that doesn't have a more specific controller and has a view, and a 404 error for anything else.