Did you know that HAML and RAILS doing really good together?

Before start why “Haml” is good with rails let me say something about “ERB”. ERB is the default template system for rails. We embed Ruby into HTML using ERB just like we do in ASP, JSP and PHP.

Following example will show how we can embed ruby in an html.erb file.

<%= form_for @product, url: {action: "create"}, html: {multipart: true} do |f| %>

As you can see in the code segment, I have created a basic form in rails using a table layout. I have used the following delimiter to embed ruby within the html file.

<%    %> 
<%= %>

when there is a huge code in an erb file, it is really annoying and I am like…

So I started using HAML instead of ERB.

HAML is a very attractive and popular alternative to ERB. HAML will eliminate repetitive tags in HTML. Ultimately it gives me a beautiful code where it is easy to read.

So, HAML is a beautiful, DRY, well-intended, clear markup

Indentation is the main key with HAML, which makes for well structured code. Sometimes you might feel unconformable when rails give indentation errors, anyway it won’t happen if you are so careful and you love this.

This is the above erb code using haml.

= form_for @product, url: {action: "create"}, html: {multipart: true} do |f|
%label{:for => "product_name"}> Name
= f.text_field :name
%label{:for => "product_price"}> Price
= f.text_field :price
%label{:for => "product_item_id"}> Item
= f.select(:item_id) do
- @items.each do |item|
= content_tag(:option, item.name, value: item.id)
%label{:for => "product_description"}> Description
= f.text_area :description, size: "60x12"
%label{:for => "product_image"}> Image
= f.file_field :image
%td{:colspan => "2"}
%td{:colspan => "2"}
= f.submit "Create"

I think you can just see the difference even by looking into the code. I like using HAML and it is super easy to handle my view files in rails. Not only the readability, its focus on cleanliness and production speed.

for more information please refer the following link

This is how I add HAML into the project

  1. First add the GEM file.

you can use the above link to read about HAML.

gem 'haml', '~> 4.0', '>= 4.0.7'


gem install haml -v 4.0.7

2. Do a bundle install, run the following command in the terminal

$bundle install

3. Done!

Now anytime you create a controller after bundle install. Rails will generate view in the formal for HAML.

Move from HTML to HAML

After adding HAML i got issue initially, How can i convert my existing html.erb to html.haml. I use following link to convert my existing code. Pretty easy.

This is something I didn’t do. but if you need to convert the whole ERB project to a HAML project. you can use a shell script. See the following link for that.

for file in $(find . -type f -name \*.html.erb); do
html2haml -e ${file} "$(dirname ${file})/$(basename ${file} .erb).haml";

or use the following gem to convert


Basics you want to know is mention bellow

.      ----> for class
# ----> for id
% ----> for tags
- ----> ruby
= ----> output in ruby

Let’s see some examples, so you can get some idea.


<strong><%= item.title %></strong>


%strong= item.title

When we add attributes into tags


<strong class="code" id="message">Hello, World!</strong>


%strong{class: "code", id: "message"} Hello, World!

If you want to add dive with a class “content”

Anything inside the div

Same thing with an ID

Anything inside the div


<div class='item' id='item<%= item.id %>'>
<%= item.body %>


.item{:id => "item#{item.id}"}= item.body

Consider using HAML next time. I mean start today :)

Senior Software Engineer ❤️ Node | Ruby