Rails Engine - Gems 依赖项,如何将它们加载到应用程序中?
我在这里做一个引擎,它独立工作正常。
当我将其转换为 gem 并将其加载到另一个应用程序中时,我收到许多未定义的错误,这些错误来自我的引擎 gem 的依赖项。
这是 gemspec:
s.add_dependency('paperclip')
s.add_dependency('jquery-rails')
s.add_dependency('rails3-jquery-autocomplete')
s.add_dependency('remotipart')
s.add_dependency('cancan')
在应用程序中,当我进行捆绑安装时,它列出了所有这些依赖项,但当我运行应用程序时,我收到很多未定义的方法错误(例如来自回形针的 has_attachment)。看来应用程序没有加载引擎依赖项。 这是默认行为吗?我可以改变它吗? 引擎内的插件也发生了同样的事情。
如果我手动插入这些 gem,在应用程序 Gemfile 中,一切正常......
I'm doing an engine here, it works alright in stand alone.
When I transform it into a gem, and load it inside another application, I get a lot of undefined errors, coming from my engine gem's dependecies.
Here is the gemspec:
s.add_dependency('paperclip')
s.add_dependency('jquery-rails')
s.add_dependency('rails3-jquery-autocomplete')
s.add_dependency('remotipart')
s.add_dependency('cancan')
In the application, when I do a bundle install, it lists all these dependencies, but as i run the application I receive a lot of undefined methods errors (has_attachment from paperclip for example). It seems that the application doesn't load the engines dependencies.
Is this the default behavior? Can I change it?
Same thing happened with a plugin inside the engine.
If I insert by hand those gems, in the application Gemfile, all works...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
将它们包含在您的 gemfile 中并运行捆绑安装。然后在您的
lib//engine.rb 文件
中需要它们。不要忘记需要 rubygems然后在你的主机应用程序(包含 gem 的应用程序)中运行bundle install/bundle update(bundle update 对我来说很有效),然后一切都应该完美运行。您还可以通过在主机应用程序中启动控制台并输入模块名称来测试这一点,例如
希望这有帮助
Include them in your gemfile and run bundle install. Then require them in your
lib/<your_engine>/engine.rb file
. Don't forget to require rubygemsThen in your host app (The app where you included your gem) run bundle install/ bundle update (bundle update did the trick for me) and then everything should work perfectly. You can also test this by starting the console in your host app and just type the module name e.g.
Hope this helps
您可以像丹尼尔发布的那样手动要求它们,也可以自动要求它们。您需要在 3 个文件中添加依赖项:
yourengine.gemspec
Gemfile
lib/yourengine.rb
更新:这是如何获取依赖项的简单演示。您应该测试它并过滤不需要的项目,例如: require d.name except d.type == :development (thx @imsinu9)
You can require them manually like Daniel posted, and you can also require them automatically. You need to add dependencies in 3 files:
yourengine.gemspec
Gemfile
lib/yourengine.rb
Update: It's a simplistic demonstration of how to require the dependencies. You should test it and filter unwanted items, for example: require d.name unless d.type == :development (thx @imsinu9)
来自 paperclip 的 README :
对于非 Rails 使用:
我遇到了同样的问题,并为我解决了它。
from paperclip's README :
For Non-Rails usage:
I had the same issue and that fixed it for me.
您必须将 gem 文件添加到 .gemspec 文件和 engine.rb 文件中。
在 .gemspec 文件中,它会是这样的:
s.add_dependency "kaminari", "0.16.1"
在engine.rb文件顶部添加:
require "kaminari"
我认为您还需要将 gem 添加到 Rails 引擎 Gemfile 并捆绑安装,但我不确定您是否需要它。
You must add the gem file to both the .gemspec file, and your engine.rb file.
In the .gemspec file it would be like:
s.add_dependency "kaminari", "0.16.1"
In the engine.rb file at the top add:
require "kaminari"
I think you also need to add the gem to the rails engine Gemfile and bundle install, but I'm not certain if you need it there.
目前(我认为 Rails 3.1 及更高版本),您不应该再在 test/dummy/Gemfile 中声明任何 gem:
引用 test/dummy/Gemfile (使用
rails plugin new my_engine 生成 -完整
):At the time being (Rails 3.1 and above I think), you shouldn't have do declare any gems in the test/dummy/Gemfile anymore:
Quote from test/dummy/Gemfile (generated using
rails plugin new my_engine --full
):您确实不应该在 Gemsec 上需要它们,并且应该加载它们。当您说“这是 gemspec”时,您是用
Gem::Specification.new do |s|
或类似的东西包围它,对吗?You really shouldn't need them on the Gemsec, and they should be loaded. When you say "here is the gemspec", you are surrounding it with
Gem::Specification.new do |s|
or something to that effect, right?您可以使用简单的捆绑器命令包含环境的所有 gem:
您可以将其添加到
config/initializer
中。You can include all gems for the environment with a simple bundler command:
You could add this to an
config/initializer
.