(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?