Search code examples
ruby-on-railsrubyhamlguardcompass

Error: uninitialized constant Guard::Haml::Engine


I am using guard with guard-haml and trying to write a RoR like render function that I can use in my haml templates.

My error is Error: uninitialized constant Guard::Haml::Engine

My Guardfile is:

# guard-haml: watch haml files in src/haml and compile to dist/
guard :haml, output: 'dist', input: 'src/haml', run_at_start: true, helper_modules: 'Helpers' do
    watch %r{^src/.+(\.html\.haml)}
end

# helpers
module ::Haml::Helpers
    def render(partial)
        Haml::Engine.new(File.read("./src/haml/_#{partial}.haml")).render
    end
end

I have a partial _head.haml and in my index.haml I have:

=render :head
%body
    %h1 Hello, World!

Not sure what to do. Anyone?


Solution

  • Change Haml::Engine to this ::Haml::Engine. Possible the code in #render is executed in Guard's namespace and makes the constant lookup to search for Guard::Haml:Engine, not Object::Haml::Engine.

    More on constant lookup in ruby.