Haml by Default in a New Rails 3.2 App

(UPDATE: Daniel Kehoe, the maintainer of the RailsApps repo, graciously accepted my contribution. I’ve updated the links to point to the master project.)

The really short version

Generate a new rails app based on a simple template that sets up Haml out of the box (and some other nice optional features).

rails new ProjectName -m https://raw.github.com/RailsApps/rails3-application-templates/master/rails3-haml-html5-template.rb

The lazy one works the hardest

I’m spinning up a new Rails app, and went looking for a shortcut to make Rails use Haml automatically for the html templates. Well, you know what they say about lazy people working hard. Sure enough, I couldn’t find a fast answer, but did find some tools to make sure I have a ready-to-roll shortcut for next time. I’m happy to share it with you now!

No built-in default to Haml

Rails 3.1 got CoffeeScript to beautify the .js files and Sass to tame stylesheet wasteland by default, but the html templates are left in the dark ages of angled brackets and irrelevant whitespace. The paucity of good google results and a few StackOverflow questions and answers made it clear that Rails doesn’t have a built in way to generate a new app with default Haml view templates. Convert the application layout by hand? Eww! Manually add the haml-rails gem? Not in 2012, no sir!

Luckily, my goal is accomplished easily enough with a little one-time elbow grease up front.

Rails app templates

Rails has long had ways of creating apps based on templates. Various methods of using these templates have sprung up, but there is one that appears to canonical for new Rails projects.  This handful of starter templates based on the inclusion of hand-picked, standardized “recipes” jumpstarts some great standard app patterns. But these offer more than what I’m looking for at the moment.

The new project I want to create will start as a simple static page, and grow from there. I don’t want to make decisions about user authentication or test suites or NoSQL stores yet. I just want a nice clean Rails app, that uses Haml. Ok, fine, I also want it to use twitter-bootstrap-sass.

Easy enough! My desired options are a subset of the more complicated recipes, so I can create my own based on what’s been done before. Away we go with the modern dev cycle: Fork, tweak, test, push, verify, pull request. Good times.

This new recipe, just like the other recipes, gives the user-selectable options (Haml or ERB? Which CSS framework? Other minor niceties), but allows me to keep my app simple for now.

To use it, just run the `rails new…` command above. But do I really want to remember that long command if this is what I want to start with every time?

Power to the .railsrc

Thanks to a little hidden feature of Rails 3.2, we can make a template like this the default whenever the `rails new …` command is run. Just add the relevant switches and options to a .railsrc file in your home directory.

# ~/.railsrc
-m https://raw.github.com/RailsApps/rails3-application-templates/master/rails3-haml-html5-template.rb

Now, the template (and any other options you might want to throw in  [see `rails –help` for the full list]) will be applied every time I run `rails new ProjectName`.

Sweet, now I get to be lazy at last!

Now show me yours

What would you want to see in your default starter Rails app template?

3 Responses to “Haml by Default in a New Rails 3.2 App”

  1. Pierre Merlin March 13, 2012 at 3:54 am #

    That’s so great… thank you so much.
    As soon as I’m able to share such a good tip, I’ll tell you.

  2. Alyssa Nicoll August 14, 2013 at 2:25 pm #

    That was so so helpful! Thank you so much!!

Trackbacks/Pingbacks

  1. Giới thiệu về Haml trong Rails - October 16, 2012

    […] tham khảo: http://en.wikipedia.org/wiki/Haml http://haml.info/tutorial.html http://decielo.com/articles/377/haml-by-default-in-a-new-rails-3-2-app […]

Leave a Reply