How do I get Haml to work with Rails?
Asked Answered
H

10

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.

Hawker answered 19/9, 2008 at 3:12 Comment(1)
What are the errors? Do you have the HAML plugin installed?Musculature
H
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.

Highbinder answered 22/5, 2010 at 12:22 Comment(2)
Add echo 'gem "haml-rails", :group => :development' >> Gemfile to use haml for scaffolding as default option.Sped
and also bundle I presumePsychodiagnostics
C
61

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    
Cassondracassoulet answered 17/2, 2011 at 21:15 Comment(2)
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.Forwardlooking
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.rbHonor
R
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
Rachitis answered 19/9, 2008 at 20:54 Comment(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. haml.hamptoncatlin.com/docs/rdoc/classes/Haml.htmlHollywood
Why is it important to end with .html.haml and not just .haml?Dronski
mathee, It's Rails convention. name.mime.format (e.g. show.html.erb, show.xml.builder, show.html.haml)Rachitis
W
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
Warder answered 8/5, 2012 at 17:55 Comment(0)
B
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>
Bly answered 21/4, 2009 at 18:44 Comment(1)
nice...that's pretty neat.Microphone
H
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.

Harrod answered 20/9, 2008 at 0:23 Comment(2)
Be mindful that for Rails 3, haml --rails is no longer needed. See my answer for Rails 3 howto.Highbinder
Please, avoid using sudo when you install gems.Taishataisho
R
3

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

Rustie answered 5/5, 2012 at 9:50 Comment(0)
K
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.

Kowalewski answered 22/10, 2009 at 21:16 Comment(0)
E
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
Endomorph answered 9/5, 2012 at 3:53 Comment(0)
F
0

make sure to add haml gem into your Gemfile

Felt answered 9/6, 2020 at 14:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.