3

我遇到了一个问题,即捆绑程序没有在 Rails 应用程序中加载单个 gem,而仅在生产中加载 - 在开发模式下使用 gem 没有问题。

gem 是country_select,我们已经在 1.1.3 版本和 github 存储库中进行了尝试。该应用程序使用的是 rails 3.2.11。

证明 gem 存在于服务器上

打包器在生产环境中成功安装 gem:

$ RAILS_ENV=production bundle install
...
Using coffee-rails (3.2.2)
Using country_select (1.1.3)
Using daemons (1.1.9)
...
Your bundle is complete! It was installed into /home/deploy/rails-app/shared/bundle

gem 出现在 bundler 安装到的共享 gem 目录中:

$ ls -al /home/deploy/rails-app/shared/bundle/ruby/1.9.1/gems/ | grep country
drwxrwxr-x  3 deploy deploy 4096 Apr  3 08:49 country_select-1.1.3

从控制台启动 rails 时会加载 gem:

$ RAILS_ENV=production script/rails c
Loading production environment (Rails 3.2.11)
irb(main):002:0> Gem.loaded_specs.keys.grep /country/
=> ["country_select"]

然而...

在 rails 应用程序中对 gem 的相同检查country_select失败。例如:

class ApplicationController < ActionController::Base

  before_filter do
    raise Gem.loaded_specs.keys.grep(/country/).inspect
  end

  #...
end

吐出[]- 即未加载 gem。另外,依赖 gem 的应用程序的某些部分由于它不存在而失败。

最后,问题

简单地在启动中添加一个require 'country_select'地方意味着 gem 被加载,并且应用程序工作。但是为什么 bundler 不使用那个特定的 gem,尽管它会安装它并将它添加到应用程序的 gem 目录中呢?

有关生产环境的更多信息:

$ rvm -v
rvm 1.18.18 (stable)...
$ ruby -v
ruby 1.9.3p392 (2013-02-22 revision 39386) [x86_64-linux]
$ gem -v
1.8.25
$ bundle -v
Bundler version 1.3.2

更新:2013 年 4 月 5 日

问题似乎已经“消失”了。经过几次重新部署后,它似乎正在工作。尽管没有更改 country_select gem,这奇怪。我仍在研究那段时间到底发生了什么变化,但我很困惑。

4

2 回答 2

0

尝试运行

RAILS_ENV=production bundle exec script/rails c
于 2013-04-04T01:00:10.830 回答
-1

我有一个使用相同版本的应用程序,我不需要在开发或生产中使用 gem。我也和你一起尝试过,before_filter并得到了["country_select"]预期的结果。所以问题不在于宝石。

我认为您要么在某个地方(不太可能)在初始化时到处乱搞加载的 gem,要么在服务器周围的设置出现问题。你是用 启动服务器RAILS_ENV=production bundle exec script/rails server吗?您能否检查服务器正在使用的 gem 路径是否包括您在问题中提到的共享 gem 路径?

于 2013-04-05T10:52:29.247 回答