Sunday, May 02, 2010

Using Haml with Rails 3.0

One of the things I [heart] about being a Ruby/Rails developer for ThoughtWorks is that I occasionally get asked to do something really cool on the side, like being a technical reviewer for an upcoming book on Rails 3.0.

In this particular book, the author uses Haml instead of Erb for templating views in the sample code. When I'm reviewing a book, I like to actually exercise as much of the sample code as I reasonably can, as it tends to be among the hardest stuff to get right, and among the easiest ways to discourage a reader when it's not right. But Rails defaults to Erb for templating, and I'm not exactly Haml-enabled.

What's a geek to do?

Well, my friend Google had a thing or two to suggest, but the process is slightly non-trivial so I thought I'd collate it here, thusly:

  • NB: All commands are entered from your Rails 3.0 application's root directory.

  • Edit [your_app]/Gemfile, and add the line gem 'haml' somewhere convenient.

  • Edit [your_app]/config/application.rb, and add the line g.template_engine :haml to the config.generators block (uncommenting the block if necessary).

  • gem install haml

  • bundle install

  • haml --rails .

  • git clone git://github.com/psynix/rails3_haml_scaffold_generator.git lib/generators/haml


That last bit is particularly important for allowing Rails' scaffold generators to Do The Haml Thang(TM) when generating view templates.

Now back to my regularly scheduled tech review ...

P.S.: The book is going to be awesome. Reserve your copy now!

No comments: