Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 280
gnerkus Avatar asked Jan 28 '16 01:01

gnerkus


1 Answers

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.

like image 117
gnerkus Avatar answered Oct 24 '22 21:10

gnerkus