使用Rails 3引擎的Gem依赖关系在初始化器中失败 [英] Gem dependencies with Rails 3 engine fail in initializer

查看:83
本文介绍了使用Rails 3引擎的Gem依赖关系在初始化器中失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<强> tldr;当我在使用引擎的测试应用程序中运行rake任务时,引擎依赖关系似乎不受尊重。我必须通过在测试应用程序的Gemfile中指定依赖关系来(-D)RY

我使用珠宝商创建了一个Rails引擎,作为宝石。它依赖于我在Rakefile中指定的Devise:

I've created a Rails Engine as a gem using Jeweler. It has a dependency on Devise which I've specified in the Rakefile:

gem.add_runtime_dependency 'devise', '~> 1.1.3'

在我的测试应用程序中,我将引擎依赖项添加到Gemfile中, bundle install 显示Devise已被选中并安装。然而,如果我运行一个初始的 rake db:migrate ,则会抛出一个错误:

In my test app I add the Engine dependency to the Gemfile, and bundle install shows that Devise is picked up and installed. However if I run an initial rake db:migrate an error is thrown:

uninitialized constant Devise
<snip>config/initializers/devise.rb:3

如果我手动将Devise依赖项添加到测试应用程序的Gemfile中,它将起作用。我可能错过了一些显而易见的东西,但是在这个阶段,测试应用程序会正确地选择并安装依赖项,但由于某种原因,初始化程序代码将失败,除非在测试应用程序的Gemfile中再次列出依赖项。

If I manually add the Devise dependency to the test app's Gemfile it works. I've probably missed something obvious, but at this stage it appears the test app picks up and installs the dependency properly, but for some reason the initializer code fails unless the dependency is listed again in the test app's Gemfile.

推荐答案

您很可能找到了解决方案或解决方法,但我会解决这个问题。

You're likely to have found a solution or a workaround, but I'll put how I solved this in case someone has the same problem.

只要放:

Just put:

require 'devise'

在lib / my_gem.rb中。

in lib/my_gem.rb .

Rails autoloads在Gemfile中指定的宝石,没有别的。如果您正在开发具有依赖性的gem,则必须手动要求它们。

Rails autoloads the gems specified in the Gemfile and nothing else. If you're developing a gem that has dependencies, you have to require them manually.

这篇关于使用Rails 3引擎的Gem依赖关系在初始化器中失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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