简体   繁体   中英

Rails console error: undefined method `each' for nil:NilClass (NoMethodError)

Any attempt to run rails console in the root directory of my Rails application produces the following error:

undefined method `each' for nil:NilClass (NoMethodError)

The full stack trace:

 /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>'

I've traced the error to this block in the railtie.rb file in the Rails lib:

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

which in turn comes from this block:

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

I uninstalled the spring gem and installed the rubocop gem. Otherwise, my Gemfile contains the default gems.

My Ruby version is 2.2.3 which is above the 2.2.2 version recommended for Rails 5.

Ruby version: 2.2.3p173 Rails version: 5.0.0.beta1.1 RVM version: 1.26.11

The error was caused by the web-console gem as referenced here: Error when running rails console . web-console was at version 3.1.0 at the time this was reported. The 3.0.0 version of web-console seemed not to have produced any errors when rails console was executed.

You'll need to ensure your version of web-console is 3.1.1 and greater as the issue has been fixed in the 3.1.1 version.

gem install spring worked for me.

My problem was:

/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>'

你需要安装spring gem

gem install spring  --pre

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM