在Rails3引擎上要求Haml的最佳方法 [英] Best way to require Haml on Rails3 engines
问题描述
我正在开发Rails3引擎应用程序,并且我想使用Haml作为视图.
I'm developing a Rails3 engine application, and I want to use Haml for the views.
首先,我要做的就是将其添加到引擎Gemfile中:
First, what I have done was to add this to the engine Gemfile:
gem "haml"
当我测试引擎时,它运行正常(我使用了 https://github.com/josevalim /enginex 生成gem并使用虚拟应用程序对其进行测试.
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).
当我尝试在真实的Rails应用程序上使用引擎时,我的问题开始了.该应用程序自身的Gemfile上没有gem"haml",因此它没有初始化Haml,所以我收到的模板找不到错误是因为它没有在寻找.haml视图.我在想,通过在引擎上要求Haml,Rails应用程序也需要它.
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.
我现在要做的是使用以下代码在引擎上添加config/initializers/haml.rb:
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)
它现在正在工作,但是我想知道这是否真的是一个好方法. 为什么Rails不会调用Haml"init.rb"文件,而是通过仅将gem"haml"添加到引擎Gemfile来正确初始化Haml?
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?
推荐答案
两件事是必要的.首先,在.gemspec中:
Two things are necessary. First, in the .gemspec:
s.add_dependency 'haml', ['>= 3.0.0']
在您的lib/gem_name.rb中:
And in your lib/gem_name.rb:
require 'haml'
然后在gem和应用程序目录中同时运行bundle
.
And then run bundle
both inside the gem and app directories.
这篇关于在Rails3引擎上要求Haml的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!