Question

I try to get haml working without the gem with sinatra (Heroku doesn't allow gem install, as far as I know)

What I've done so far:

  • clone the haml git repo inside my project

  • add : require 'haml/lib/haml.rb' to my sinatra main file

the following works:

get '/test' do
  Haml::Engine.new('%p test').render
end

but the following doesn't:

get '/test2' do
  haml :my_template
end

I get the error :

NoMethodError - undefined method each' for nil:NilClass (haml):20:inrender'

./haml/lib/haml/engine.rb:152:in `render'

./haml/lib/haml/engine.rb:152:in `instance_eval'

./haml/lib/haml/engine.rb:152:in `render' ...

Is there any other files to require ? Any ideas ?

Was it helpful?

Solution

Heroku supports installing gems by creating a .gems file in your project's root directory and adding it to Git. On your next push the gems required in there will be installed. To install Haml the file would contain this line:

haml --version '>= 2.2.0'

More information can be found here: http://docs.heroku.com/gems

OTHER TIPS

The two most obvious possibilities (in order of probability):

  1. There's an error in some Ruby code in your Haml file. If you can try the same code outside of Sinatra, does it render OK there?
  2. The file my_template.haml is either not there or incorrectly named.

As StuFF mc has indicated, in response to petergassner's answer, the correct way to do this now is to use your app's Gemfile. In that Gemfile you would do

gem "haml", ">= 2.2.0"

Heroku knows when you push your app to install the gems mentioned in your gemfile.

Note that if you want to use sass as well as haml, you may need to jump through a couple of hoops to get that set up. The way haml used to work traditionally involved writing the compiled css files to disk, which is a problem on heroku's read only filesystem. Someone eventually wrote a plugin/gem to solve this situation (sass-on-herku or some such).

Currently, the haml-rails gem seems to take care of this using the asset pipeline, but if you're using Sinatra, this likely won't work for you. So, you'll either want to see if one of those gems is still in operation, or take the advice that heroku seems to currently be espousing. This article is also rails specific, but you should be able to get things to work appropriately for Sinatra without too much difficulty. (Basically, to summarize the point here, heroku does now support writing to a tmp directory, so as long as you can configure both the sass gem and the Sinatra app to use some tmp location for this, you should be good.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top