views:

1058

answers:

4

I'm getting Ruby on Rails set up on a fresh installation of Snow Leopard. After battling (and beating) MySQL and Sphinx problems, I'm stuck on a stupid error related to HAML.

Essentially I'm getting a missing template error for every view that uses HAML. I can add a blank xxx.html.erb file and and a (blank) page loads fine. But xxx.html.haml throws the error, even though that file definitely exists in the appropriate directory.

Here is the error from the development server:

ActionView::MissingTemplate (Missing template sections/index.erb in view path app/views):
  haml (2.2.4) rails/./lib/sass/plugin/rails.rb:19:in `process'

I am using the haml gem (2.2.4), rails gem (2.3.4), and ruby 1.8.7. I did run haml --rails . from my RoR app root directory; the init file is in the vendor/plugins/haml directory. I have confirmed that "require 'haml'" => true via IRB.

Any help would be appreciated!

In response to Yaraher:

Tried un- and re-installing, which raised no errors except with the rdocs ("Could not find main page README.rdoc").

In script/console:

>> require 'haml'
=> []

Update:

Installing an old version of Rails known to work with HAML in this project seems to have "fixed" the problem. I'd still be curious to hear a real answer to this problem -- I don't want to be stuck at 2.3.2.

sudo gem install -v 2.3.2 rails

Update 2:

This is definitely caused by a difference between Rails 2.3.2 and 2.3.4. With both gems installed I can set which is used in my environment.rb file. HAML works fine with 2.3.2 and breaks as described with 2.3.4.

A: 

Try to rename you template to

sections/_index.haml

or

sections/__index.haml
Lichtamberg
Doesn't work. Wouldn't this just tag the templates as partials?
Max Masnick
A: 

Time for the embarrassing answer to this question:

There is a difference between Rails 2.3.2 and 2.3.4 that does not recognize xxx.haml.html files as files that should be parsed by HAML (or ERB); this worked fine in 2.3.2. HAML files should always be named xxx.html.haml anwyay, so this isn't even a bug.

I didn't realize that this was the problem because I had the "hide file extensions" option turned on (on by default) in my fresh Snow Leopard installation. So when I tried switching the file extension through Finder to html.haml to test this, it actually changed it to haml.html.haml or some nonsense.

Moral of the story: I ran the following script and now HAML works with Rails 2.3.4.

path = '/path_to_rails_app/views/'
dir = Dir.new(path)
dir.each do |d|
  if File.directory?(path+d)
    Dir.new(path+d).each do |f|
      if (f =~ /.*\.haml\.html$/) != nil
        File.rename(path+d+'/'+f, path+d+'/'+f.gsub('haml.html', 'html.haml'))
      end
    end
  end
end
Max Masnick
A: 

Wow Max thanks for your fix, but, where did you run that script, in the controller or in console, 'cause it's still not working for me :)

I'm in Mac OS and Leopard too

Thanks again

dgfrancisco
The Ruby script in the answer renames xxx.haml.html to xxx.html.haml in case that wasn't clear. To run it, save it on your desktop as "fix_haml.rb" using your favorite plain text editor, and then change path_to_rails_app to the path of your rails app. Finally, open up terminal and type "ruby ~/Desktop/fix_haml.rb". That should do it!
Max Masnick
A: 

After many attempts of having this problem with renaming a pre-haml installed html.erb file, i was able to generate a new scaffold and rename the file successfully.

It should be noted that directly before creating the new scaffold, I have added require 'haml' to my environments.rb file and I also restarted my IDE (aptana) all together. It still won't work for the existing html files, but it worked for all new ones.

I guess maybe the solution is to install haml immediately after starting a project and before generating any code. Is there a way to automatically include haml when new projects are created?

nelson