问题
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.
回答1:
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
回答2:
Try to rename your template to
sections/_index.haml
or
sections/__index.haml
来源:https://stackoverflow.com/questions/1423935/actionviewmissingtemplate-with-haml