Rails scaffold, create .haml rather than .erb views

After being tired of refactoring .erb files to .haml I headed for a way to modify the rails generator that will create .haml view files rather than .erb view files.

After a while, I found what exactly I needed. ‘haml-rails’ is the name of the solution. This is how you do it.

Add the following line to your Gemfile:

gem 'haml-rails'

Then run the bundler (bundle install).

Now append the following lines in your application.rb file (at the end of Application class):

config.generators do |g|
   g.template_engine :haml
end

That’s it. Your new scaffolds will generate .haml files (and obviously haml syntaxes) for you.

This is much easier than Haml-scaffold.

  • Nhm tanveer

    Hi don’t limit Haml gem to development. You can’t use it on production mode then gem won’t load.

  • The HungryCoder

    Yes, you are right for this code fragment. However, it was ok for me as I already had

    gem 'haml'

    in my Gemfile. That was enabling .haml in production. But I overlooked the issue that two gems were not needed for this purpose. Thanks for spotting :).

  • almirsarajcic

    Great post, thanks.