Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
658 views
in Technique[技术] by (71.8m points)

ruby on rails - Best way to require Haml on Rails3 engines

I'm developing a Rails3 engine application, and I want to use Haml for the views.

First, what I have done was to add this to the engine Gemfile:

gem "haml"

While I was testing my engine, it was working OK (I have used https://github.com/josevalim/enginex to generate the gem and test it with the dummy application).

My problems started when I tried to use the engine on a real Rails application. The application does not have gem "haml" on it's own Gemfile, and so it was not initializing Haml, so I was receiving template not found errors as it was not looking for the .haml views. I was thinking that by requiring Haml on the Engine it would be enought for it to be also required by the Rails application.

What I have done for now was to add a config/initializers/haml.rb on the engine with this code:

require 'haml'
Haml.init_rails(binding)

It's working now, but I'm wondering if this is really a good way to do it. Why Rails is not calling Haml "init.rb" file and so initializing Haml correctly by just adding gem "haml" to the engine Gemfile?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Two things are necessary. First, in the .gemspec:

s.add_dependency 'haml', ['>= 3.0.0']

And in your lib/gem_name.rb:

require 'haml'

And then run bundle both inside the gem and app directories.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...