Gem 的未定义方法‘ source_index’: Module (NoMethodError)

我正在运行一个 Rails 2.3.5应用程序,在运行 script/server 时,我看到了以下内容:

./script/../config/../vendor/rails/railties/lib/rails/gem_dependency.rb:21:in `add_frozen_gem_path': undefined method `source_index' for Gem:Module (NoMethodError)
from ./script/../config/boot.rb:60:in `load_initializer'
from ./script/../config/boot.rb:44:in `run'
from ./script/../config/boot.rb:17:in `boot!'
from ./script/../config/boot.rb:123
from script/server:2:in `require'
from script/server:2

如果我在 boot.rb 中注释掉第60行(Rails: : GemDependency.add _ zen _ gem _ path)并运行 script/server,我会得到:

=> Booting WEBrick
=> Rails 2.3.5 application starting on http://0.0.0.0:3000
./script/../config/../vendor/rails/railties/lib/rails/gem_dependency.rb:21:in `add_frozen_gem_path': undefined method `source_index' for Gem:Module (NoMethodError)
from ./script/../config/../vendor/rails/railties/lib/initializer.rb:298:in `add_gem_load_paths'
from ./script/../config/../vendor/rails/railties/lib/initializer.rb:132:in `process'
from ./script/../config/../vendor/rails/railties/lib/initializer.rb:113:in `send'
from ./script/../config/../vendor/rails/railties/lib/initializer.rb:113:in `run'
from /home/developer/bigpink/config/environment.rb:13
from /home/developer/bigpink/vendor/rails/activesupport/lib/active_support/dependencies.rb:156:in `require'
from /home/developer/bigpink/vendor/rails/activesupport/lib/active_support/dependencies.rb:156:in `require'
from /home/developer/bigpink/vendor/rails/activesupport/lib/active_support/dependencies.rb:521:in `new_constants_in'
from /home/developer/bigpink/vendor/rails/activesupport/lib/active_support/dependencies.rb:156:in `require'
from /home/developer/bigpink/vendor/rails/railties/lib/commands/server.rb:84
from script/server:3:in `require'
from script/server:3

所以,我真的不知道该怎么办。希望我能得到一些快速的帮助。谢谢!

68864 次浏览

我自己在试图将一个旧的 Rails 应用程序从 REE 1.8.7升级到1.9.3-p385时也遇到了这个问题。奇怪的是,Ruby 1.9.3-p327运行正常。归结起来就是 ruby-1.9.3-p385为我安装了 RubyGems 版本2.0.2,1.9.3-p327安装了 RubyGems 版本1.8.23。

Gem.source_index已经被废弃了一段时间,但是由于 Rails 2.3除了关键的安全补丁之外没有得到任何更新,所以这个问题永远不会得到解决。RubyGems v2.0最终删除了该方法。降级到2.0.0之前的任何 rubygems版本,比如1.8.25,以获得目前的功能。您可以使用 gem update --system 1.8.25获得兼容的版本。

作为非常重要的一点,Rails 2.3.5需要更新到至少2.3.17。有一些关键的安全漏洞会让你受到一些非常恶劣的攻击。从长远来看,升级到3.x 需要被认为是一个非常强烈的需求。

这帮助了我: http://djellemah.com/blog/2013/02/27/rails-23-with-ruby-20/

我把这个和 gem update --system 1.8.25结合起来做了,你的案子可能不需要这个。

对于 rvm 用户,

rvm install rubygems 1.8.2 --force

另一种方法是安装 Slimgems: gem install slimgems。这是 RubyGems 的一个插件分支,它可以更好地与旧版本一起工作。

更新: 除了@uxp 应答外,如果在运行 Catalina 的 Mac 上运行此命令,则需要添加-n。

所以命令应该是 sudo gem update --system -n 1.8.25