Rails控制台错误:未定义的方法`每个' for nil:NilClass(NoMethodError)

时间:2016-01-28 01:00:42

标签: ruby-on-rails ruby ruby-on-rails-5

在我的Rails应用程序的根目录中运行rails console的任何尝试都会产生以下错误:

  

未定义的方法`每个' for nil:NilClass(NoMethodError)

完整堆栈跟踪:

 /Users/gnerkus/.rvm/gems/ruby-2.2.3@rails-experiments/gems/railties-5.0.0.beta1.1/lib/rails/railtie.rb:245:in `each_registered_block': undefined method `each' for nil:NilClass (NoMethodError)
  from /Users/gnerkus/.rvm/gems/ruby-2.2.3@rails-experiments/gems/railties-5.0.0.beta1.1/lib/rails/railtie.rb:224:in `run_console_blocks'
  from /Users/gnerkus/.rvm/gems/ruby-2.2.3@rails-experiments/gems/railties-5.0.0.beta1.1/lib/rails/application.rb:461:in `block in run_console_blocks'
  from /Users/gnerkus/.rvm/gems/ruby-2.2.3@rails-experiments/gems/railties-5.0.0.beta1.1/lib/rails/engine/railties.rb:13:in `each'
  from /Users/gnerkus/.rvm/gems/ruby-2.2.3@rails-experiments/gems/railties-5.0.0.beta1.1/lib/rails/engine/railties.rb:13:in `each'
  from /Users/gnerkus/.rvm/gems/ruby-2.2.3@rails-experiments/gems/railties-5.0.0.beta1.1/lib/rails/application.rb:461:in `run_console_blocks'
  from /Users/gnerkus/.rvm/gems/ruby-2.2.3@rails-experiments/gems/railties-5.0.0.beta1.1/lib/rails/engine.rb:442:in `load_console'
  from /Users/gnerkus/.rvm/gems/ruby-2.2.3@rails-experiments/gems/railties-5.0.0.beta1.1/lib/rails/commands/console.rb:34:in `initialize'
  from /Users/gnerkus/.rvm/gems/ruby-2.2.3@rails-experiments/gems/railties-5.0.0.beta1.1/lib/rails/commands/console_helper.rb:9:in `new'
  from /Users/gnerkus/.rvm/gems/ruby-2.2.3@rails-experiments/gems/railties-5.0.0.beta1.1/lib/rails/commands/console_helper.rb:9:in `start'
  from /Users/gnerkus/.rvm/gems/ruby-2.2.3@rails-experiments/gems/railties-5.0.0.beta1.1/lib/rails/commands/commands_tasks.rb:78:in `console'
  from /Users/gnerkus/.rvm/gems/ruby-2.2.3@rails-experiments/gems/railties-5.0.0.beta1.1/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
  from /Users/gnerkus/.rvm/gems/ruby-2.2.3@rails-experiments/gems/railties-5.0.0.beta1.1/lib/rails/command.rb:20:in `run'
  from /Users/gnerkus/.rvm/gems/ruby-2.2.3@rails-experiments/gems/railties-5.0.0.beta1.1/lib/rails/commands.rb:19:in `<top (required)>'
  from bin/rails:4:in `require'
  from bin/rails:4:in `<main>'

我已在Rails lib的railtie.rb文件中跟踪此块的错误:

def each_registered_block(type, &block)
  klass = self.class
  while klass.respond_to?(type)
    # The ':console' type doesn't seem to exist
    klass.public_send(type).each(&block) 
    klass = klass.superclass
  end
end

反过来来自这个块:

def run_console_blocks(app) #:nodoc:
  each_registered_block(:console) { |block| block.call(app) }
end

我卸载了spring gem并安装了rubocop gem。否则,我的Gemfile包含默认宝石。

我的Ruby版本为2.2.3,高于推荐用于Rails 5的2.2.2版本。

Ruby版本: 2.2.3p173 Rails版本: 5.0.0.beta1.1 RVM版本: 1.26.11

3 个答案:

答案 0 :(得分:5)

错误是由此处引用的web-console gem引起的:Error when running rails console。报告此时web-console版本为3.1.0。执行3.0.0时,web-consolerails console似乎没有产生任何错误。

您需要确保web-console版本3.1.1更高,因为问题已在3.1.1版本中修复。

答案 1 :(得分:0)

gem install spring为我工作。

我的问题是:

/usr/local/bin/NMONMerge.rb:209:in `block in <main>': undefined method `each' for nil:NilClass (NoMethodError)
from /usr/local/bin/NMONMerge.rb:191:in `each'
from /usr/local/bin/NMONMerge.rb:191:in `<main>'

答案 2 :(得分:-2)

您需要安装spring gem

gem install spring  --pre