3

我问这个的目的也是为了提供答案,因为我在弄清楚出了什么问题时遇到了很多麻烦,而谷歌并没有太多帮助 - 但是,由于这是 StackOverflow,我相信有人会更胜任地回答或改进答案。

我需要为我正在开发的 Rails 应用程序升级 gem,它引入了两个新的依赖项,其中一个是mime-types. 在本地工作,我将以下行添加到config/environment.rb

config.gem 'mime-types', :version => '1.17'

为了在我们的服务器上进行部署,所有 gem 依赖项都必须安装在 中vendor/gems,因此我将mime-typesgem 安装到当前的 rvm gemset 中:

gem install mime-types --version 1.17

...并将其版本解压缩为vendor/gems

rake gems:unpack
rake gems:unpack:dependencies

但是,在服务器上部署后,发现rake db:migrate运行时出现如下错误,提示mime-types找不到gem(stack trace部分省略):

no such file to load -- mime-types
/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `require'
[...]
/lib/tasks/misc.rake:4
/usr/lib/ruby/1.8/rake.rb:636:in `call'
/usr/lib/ruby/1.8/rake.rb:636:in `execute'
/usr/lib/ruby/1.8/rake.rb:631:in `each'
/usr/lib/ruby/1.8/rake.rb:631:in `execute'
/usr/lib/ruby/1.8/rake.rb:597:in `invoke_with_call_chain'
/usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'
/usr/lib/ruby/1.8/rake.rb:590:in `invoke_with_call_chain'
/usr/lib/ruby/1.8/rake.rb:607:in `invoke_prerequisites'
/usr/lib/ruby/1.8/rake.rb:604:in `each'
/usr/lib/ruby/1.8/rake.rb:604:in `invoke_prerequisites'
/usr/lib/ruby/1.8/rake.rb:596:in `invoke_with_call_chain'
/usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'
/usr/lib/ruby/1.8/rake.rb:590:in `invoke_with_call_chain'
/usr/lib/ruby/1.8/rake.rb:583:in `invoke'
/usr/lib/ruby/1.8/rake.rb:2051:in `invoke_task'
/usr/lib/ruby/1.8/rake.rb:2029:in `top_level'
/usr/lib/ruby/1.8/rake.rb:2029:in `each'
/usr/lib/ruby/1.8/rake.rb:2029:in `top_level'
/usr/lib/ruby/1.8/rake.rb:2068:in `standard_exception_handling'
/usr/lib/ruby/1.8/rake.rb:2023:in `top_level'
/usr/lib/ruby/1.8/rake.rb:2001:in `run'
/usr/lib/ruby/1.8/rake.rb:2068:in `standard_exception_handling'
/usr/lib/ruby/1.8/rake.rb:1998:in `run'
/usr/bin/rake:28
no such file to load -- mime-types
/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `require'
[...]
/lib/tasks/misc.rake:4
/usr/lib/ruby/1.8/rake.rb:636:in `call'
/usr/lib/ruby/1.8/rake.rb:636:in `execute'
/usr/lib/ruby/1.8/rake.rb:631:in `each'
/usr/lib/ruby/1.8/rake.rb:631:in `execute'
/usr/lib/ruby/1.8/rake.rb:597:in `invoke_with_call_chain'
/usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'
/usr/lib/ruby/1.8/rake.rb:590:in `invoke_with_call_chain'
/usr/lib/ruby/1.8/rake.rb:607:in `invoke_prerequisites'
/usr/lib/ruby/1.8/rake.rb:604:in `each'
/usr/lib/ruby/1.8/rake.rb:604:in `invoke_prerequisites'
/usr/lib/ruby/1.8/rake.rb:596:in `invoke_with_call_chain'
/usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'
/usr/lib/ruby/1.8/rake.rb:590:in `invoke_with_call_chain'
/usr/lib/ruby/1.8/rake.rb:583:in `invoke'
/usr/lib/ruby/1.8/rake.rb:2051:in `invoke_task'
/usr/lib/ruby/1.8/rake.rb:2029:in `top_level'
/usr/lib/ruby/1.8/rake.rb:2029:in `each'
/usr/lib/ruby/1.8/rake.rb:2029:in `top_level'
/usr/lib/ruby/1.8/rake.rb:2068:in `standard_exception_handling'
/usr/lib/ruby/1.8/rake.rb:2023:in `top_level'
/usr/lib/ruby/1.8/rake.rb:2001:in `run'
/usr/lib/ruby/1.8/rake.rb:2068:in `standard_exception_handling'
/usr/lib/ruby/1.8/rake.rb:1998:in `run'
/usr/bin/rake:28
Missing these required gems:
  mime-types = 1.17

You're running:
  ruby 1.8.7.302 at /usr/bin/ruby1.8
  rubygems 1.3.7 at /home/mark/.gem/ruby/1.8, /var/lib/gems/1.8

Run `rake gems:install` to install the missing gems.

但是,mime-types出现在vendor/gems/mime-types-1.17. 为什么 rake 不认为在这种情况下安装了 gem?

4

2 回答 2

4

一位同事帮我解决了这个问题:Rails 似乎希望所有的 gemvendor/gems都有一个特定的布局,这mime-types不符合。解决方案是更改environment.rb指定依赖项的行mime-type以添加:lib选项,该选项应指定指示如何获取 gem 中源的相对路径:

config.gem 'mime-types', :version => '1.17', :lib => 'mime/types'

这意味着查找vendor/gems/mime-types-1.17/lib/mime/types而不是基于 gem 名称的默认值,vendor/gems/mime-types-1.17/lib/mime-types/

于 2012-06-28T16:53:23.867 回答
1

require 'mime-types'只需在您的 ruby​​ 文件中删除即可。

正如mime-types在红宝石中构建的那样。你不需要安装它的 gem 或者明确地包含它。

就用它。这对我有用。

于 2015-09-09T06:44:00.310 回答