views:

2445

answers:

8

Rails is suddenly trying to render ERB instead of Haml and I can't figure out why. I've created new rails projects, reinstalled Haml, and reinstalled Rails.

Here's exactly the steps I take when making my application (Rails 2.3.2):

  rails> rails test
  rails> cd test
  rails\test> haml --rails .
  rails\test> ruby script\generate model user email:string password:string
  rails\test> ruby script\generate controller users index
  rails\test> rake db:migrate

Here's what the UsersController looks like:

class UsersController < ApplicationController
  def index
    @users = User.all
  end
end

My routes:

ActionController::Routing::Routes.draw do |map|
  map.resources :users
end

I now create views\users\index.html.haml:

%table
  %th(style="text-align: left;")
    %h1 Users
  - for user in @users
    %tr
      %td= user.email
      %td= user.password

Annnd run the server... I navigate to localhost:3000\users and I get this error message:

Template is missing

Missing template users/index.erb in view path app/views

For some reason Rails is trying to find and render .erb files instead of .haml files. vendor\plugins\haml\init.rb exists, untouched.

I've reinstalled Haml (Pretty Penny) multiple times and still get the same results. I've also tried adding config.gem 'haml' to my environment.rb but this also doesn't work.

I can't figure out why suddenly rails will not render haml for me.

+6  A: 

Hi it seem like haml is not enabled as Rails plugin ,in order to enable it use the following command .

Go to your application folder from the command prompt type the following

  $ cd ..
  $ haml --rails <yourproject>

if this doesnot work try installing haml gem with the following code

$ gem install haml

I tried with above example , it did work for me ,i have haml gem installed in my ubuntu system .

Good luck !

YetAnotherCoder
As stated above, I already tried this. I'm not having problems anymore, though. I had to reinstall rails twice to get it to work.Thank you, though =)
c00lryguy
A: 

Hmmm strange, this might be related.

According to: http://www.ruby-forum.com/topic/101346 you should use resource_url helpers in controllers and resource_path helpers in views. Right?

BUT, if I do use a resource_url helper in a redirect_to call inside my controller, then I get:

Missing template htp://localhost:4000/categories/new.erb in view path app/views

If I use the resource_path helper instead, there aren't any problems at all. Anyone knows what could be wrong?

Why is the resource_url helper trying to redirect to an .erb file?

This is the error from the server log:

ActionView::MissingTemplate (Missing template http://localhost:4000/categories/new.erb in view path app/views):
haml (2.2.2) lib/haml/helpers/action_view_mods.rb:13:in `render'
app/controllers/categories_controller.rb:15:in `create'
haml (2.2.2) rails/./lib/sass/plugin/rails.rb:19:in `process'

P.S. This is in Rails 2.3.3

A: 

maybe your file name is wrong, if you have a whitespace in the end of the index.html.haml_, rails will wrong...

lfx_cool
Nope, I changed nothing in the Rails application directory.. I just reinstalled Rails and it worked *shrug*
c00lryguy
A: 

Oh thank you, but the filename is not wrong. I think there is a bug in Rails.

lobo_tuerto
Out of curiosity, did you use a third-party scaffolding gem to create your views? I just installed Rails on a new Ubuntu installation and added HAML as a plugin. I then installed the haml-scaffolding gem and it named one of the templates wrong and I got the error again. If the template or haml file isn't named /exactly/ correctly, it'll throw an error.
c00lryguy
Nope, just the pure haml gem.But I don't think I have the filename wrong, as I said, if I use the "resource_path" helpers it renders ok, the problem arises only if I use the "resource_url" helper.Will try again, with the next version of Rails.
lobo_tuerto
A: 

I name all of my haml file only .haml

f.e.

test.haml
# not
test.html.haml
Lichtamberg
It never mattered for me.. Rails always rendered .html.haml and .haml files
c00lryguy
A: 

I ran into the same problem and I had to restart my server after installing Haml before my rails app recognized the changes.

ejunker
+2  A: 

I had this same problem (see this post) with Rails 2.3.4. Multiple gem uninstall/gem install rails didn't fix the problem. But downgrading to Rails 2.3.2 worked! (I know HAML previously worked in this project with this version of Rails).

sudo gem install -v 2.3.2 rails
Max Masnick
I've confirmed that I don't have HAML problems when I specify Rails 2.3.2 in my environment.rb, but do have problems with 2.3.4 (I installed both versions so I could switch between them).
Max Masnick
Fixed my problem. It was a html.haml vs. haml.html file extension issue. See the post referenced in this answer for details.
Max Masnick
+1  A: 

It's worth noting that the fact that the error message says that it couldn't find index.erb doesn't mean that it didn't look for index.haml too. The erb extension is hard-coded into the error message.

I thought that I had the same problem you describe, but it turned out that my application simply couldn't find my partial at all - it had nothing to do with the file extension.

action_ben