77

I am trying to get Haml to work with my Ruby on Rails project. I am new to Ruby on Rails and I really like it. However, when I attempt to add an aplication.html.haml or index.html.haml for a view, I just receive errors.

I am using NetBeans as my IDE.

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76

10 Answers10

88

Haml with Rails 3

For Rails 3 all you need to do is add gem "haml", '3.0.25' to your Gemfile. No need to install plugin or run haml --rails ..

Just:

$ cd awesome-rails-3-app.git
$ echo 'gem "haml"' >> Gemfile

And you're done.

Tim Scollick
  • 1,242
  • 1
  • 16
  • 17
kch
  • 77,385
  • 46
  • 136
  • 148
60

The answers above are spot-on. You just need to put gem 'haml' in your Gemfile.

One other tip that was not mentioned: to have rails generators use haml instead of erb, add the following to config/application.rb:

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

  # you can also specify a different test framework or ORM here
  # g.test_framework  :rspec
  # g.orm             :mongoid
end    
bowsersenior
  • 12,524
  • 2
  • 46
  • 52
  • 31
    Alternatively to editing `application.rb`, you can add `gem 'haml-rails'` to the development group in `Gemfile`. That will also take care of the generators. – Felix Rabe Jan 04 '12 at 13:00
  • 1
    this is equivalent to `config.generators.template_engine :haml` although the do block is nice if you want to customize a number of generators. And to reiterate, if you use `gem 'haml-rails'` it should solve this problem without needing a line of code in `config/application.rb` – Ryan Taylor May 24 '13 at 19:52
37

First, install haml as a gem in bundler by adding this to your Gemfile:

gem "haml"

Run bundle install, then make sure your views are named with a *.html.haml extension. For example:

`-- app
    `-- views
        |-- layouts
        |   `-- application.html.haml
        `-- users
            |-- edit.html.haml
            |-- index.html.haml
            |-- new.html.haml
            `-- show.html.haml
Ryan McGeary
  • 235,892
  • 13
  • 95
  • 104
  • 3
    If you have erb templates already go ahead and rename them .html.haml. At the top of the file tell haml to use the erb filter by putting :erb at the top of the file. Then you can slowly convert your templates. More on filters by visiting. http://haml.hamptoncatlin.com/docs/rdoc/classes/Haml.html – gregf Apr 21 '09 at 22:56
  • 4
    Why is it important to end with .html.haml and not just .haml? – boo-urns Apr 26 '10 at 23:39
  • 12
    mathee, It's Rails convention. `name.mime.format` (e.g. show.html.erb, show.xml.builder, show.html.haml) – Ryan McGeary Apr 29 '10 at 12:34
25

Add haml to your Gemfile:

gem "haml"

If you want to use the scaffold-functions too, add haml-rails within your development-group:

gem 'haml-rails', :group => :development

Don't forget to run:

$ bundle install
likethesky
  • 846
  • 3
  • 12
  • 28
Matthias
  • 10,816
  • 2
  • 19
  • 12
11

Before trying to use haml in your rails application, you can verify that the command line executable is installed correctly:

$ haml
%p 
  %span Hello World!

Then press CTRL-D and you should see:

<p>
  <span>Hello World!</span>
</p>
gdelfino
  • 11,053
  • 6
  • 44
  • 48
10

First, make sure you have the HAML gem.

gem list --local | grep haml

If haml doesn't show up in the list, then do this:

sudo gem install haml

Then do this from your project directory:

# cd ../
# haml --rails <yourproject>

That should install everything you need, and the HAML views should stop complaining and parse correctly.

Pete
  • 1,517
  • 11
  • 19
3

This may be an old question but I think the answer is using haml-rails at https://github.com/indirect/haml-rails

thd
  • 2,380
  • 1
  • 25
  • 33
1

if for some reason you installed haml, but you haml doesn't start. try

sudo ln haml /usr/bin/

in the bin directory of your haml gem

for some reason this didn't happen automatically on my ubuntu 9.04 Jaunty.

sra
  • 23,820
  • 7
  • 55
  • 89
0

If you are using Pow you will need to restart it also. Ideally you are using powder (gem install powder), because then you can just run this at the terminal

$ powder restart
chris raethke
  • 425
  • 3
  • 8
0

make sure to add haml gem into your Gemfile

hsul4n
  • 491
  • 7
  • 15