Rails Engine - Gems 依赖项,如何将它们加载到应用程序中? [英] Rails Engine - Gems dependencies, how to load them into the application?

查看:29
本文介绍了Rails Engine - Gems 依赖项,如何将它们加载到应用程序中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在这里做一个引擎,它可以独立工作.

I'm doing an engine here, it works alright in stand alone.

当我将其转换为 gem 并将其加载到另一个应用程序中时,我收到了很多未定义的错误,这些错误来自于我的引擎 gem 的依赖项.

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.

这是 gemspec:

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')

在应用程序中,当我进行捆绑安装时,它会列出所有这些依赖项,但是当我运行应用程序时,我收到了许多未定义的方法错误(例如来自回形针的 has_attachment).应用程序似乎没有加载引擎依赖项.这是默认行为吗?我可以改变它吗?引擎内的插件也发生了同样的事情.

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.

如果我手动插入这些宝石,在应用程序 Gemfile 中,一切正常...

If I insert by hand those gems, in the application Gemfile, all works...

推荐答案

将它们包含在您的 gemfile 中并运行 bundle install.然后在您的 lib//engine.rb 文件 中引入它们.不要忘记 require ruby​​gems

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 rubygems

  require 'rubygems'
  require 'paperclip'
  require 'jquery-rails'
  require 'rails3-jquery-autocomplete'
  require 'remotipart'
  require 'cancan'

然后在您的主机应用程序(包含您的 gem 的应用程序)中运行 bundle install/bundle update(bundle update 对我有用),然后一切都应该正常运行.您还可以通过在您的主机应用程序中启动控制台来测试这一点,只需输入模块名称,例如

Then 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.

Loading development environment (Rails 3.0.3)
irb(main):001:0> Paperclip
=> Paperclip

希望能帮到你

这篇关于Rails Engine - Gems 依赖项,如何将它们加载到应用程序中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆