Search code examples
ruby-on-railsrubyruby-on-rails-5rackactionpack

New error while upgrading to Rails 5: NoMethodError (undefined method `id' for {}:Hash)


I recently upgraded to Rails 5. After upgrading and the fixing the usual things, my application appeared to be running correctly and serving responses.

Even though my test suite passes with 94% coverage, when performing an actual request, the controller crashes after claiming the request was a 200 OK.

It looks like the stack trace does not point to any code in my application, but rather something inside of Actionpack.

Is this a misconfiguration on my part, or a legitimate bug within Rails 5?

Completed 200 OK in 141ms (Views: 2.2ms | ActiveRecord: 124.9ms)

    NoMethodError (undefined method `id' for {}:Hash):

    actionpack (5.0.0.1) lib/action_dispatch/request/session.rb:70:in `id'
    rack (2.0.1) lib/rack/session/abstract/id.rb:341:in `commit_session'
    rack (2.0.1) lib/rack/session/abstract/id.rb:224:in `context'
    rack (2.0.1) lib/rack/session/abstract/id.rb:216:in `call'
    actionpack (5.0.0.1) lib/action_dispatch/middleware/cookies.rb:613:in `call'
    actionpack (5.0.0.1) lib/action_dispatch/middleware/callbacks.rb:38:in `block in call'
    activesupport (5.0.0.1) lib/active_support/callbacks.rb:97:in `__run_callbacks__'
    activesupport (5.0.0.1) lib/active_support/callbacks.rb:750:in `_run_call_callbacks'
    activesupport (5.0.0.1) lib/active_support/callbacks.rb:90:in `run_callbacks'
    actionpack (5.0.0.1) lib/action_dispatch/middleware/callbacks.rb:36:in `call'
    actionpack (5.0.0.1) lib/action_dispatch/middleware/executor.rb:12:in `call'
    actionpack (5.0.0.1) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
    actionpack (5.0.0.1) lib/action_dispatch/middleware/debug_exceptions.rb:49:in `call'
    actionpack (5.0.0.1) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
    railties (5.0.0.1) lib/rails/rack/logger.rb:36:in `call_app'
    railties (5.0.0.1) lib/rails/rack/logger.rb:24:in `block in call'
    activesupport (5.0.0.1) lib/active_support/tagged_logging.rb:70:in `block in tagged'
    activesupport (5.0.0.1) lib/active_support/tagged_logging.rb:26:in `tagged'
    activesupport (5.0.0.1) lib/active_support/tagged_logging.rb:70:in `tagged'
    railties (5.0.0.1) lib/rails/rack/logger.rb:24:in `call'
    actionpack (5.0.0.1) lib/action_dispatch/middleware/request_id.rb:24:in `call'
    rack (2.0.1) lib/rack/method_override.rb:22:in `call'
    rack (2.0.1) lib/rack/runtime.rb:22:in `call'
    activesupport (5.0.0.1) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
    actionpack (5.0.0.1) lib/action_dispatch/middleware/executor.rb:12:in `call'
    actionpack (5.0.0.1) lib/action_dispatch/middleware/static.rb:136:in `call'
    rack-cors (0.4.0) lib/rack/cors.rb:80:in `call'
    rack (2.0.1) lib/rack/sendfile.rb:111:in `call'
    railties (5.0.0.1) lib/rails/engine.rb:522:in `call'
    thin (1.7.0) lib/thin/connection.rb:86:in `block in pre_process'
    thin (1.7.0) lib/thin/connection.rb:84:in `catch'
    thin (1.7.0) lib/thin/connection.rb:84:in `pre_process'
    thin (1.7.0) lib/thin/connection.rb:53:in `process'
    thin (1.7.0) lib/thin/connection.rb:39:in `receive_data'
    eventmachine (1.2.1) lib/eventmachine.rb:194:in `run_machine'
    eventmachine (1.2.1) lib/eventmachine.rb:194:in `run'
    thin (1.7.0) lib/thin/backends/base.rb:73:in `start'
    thin (1.7.0) lib/thin/server.rb:162:in `start'
    rack (2.0.1) lib/rack/handler/thin.rb:22:in `run'
    rack (2.0.1) lib/rack/server.rb:296:in `start'
    railties (5.0.0.1) lib/rails/commands/server.rb:79:in `start'
    railties (5.0.0.1) lib/rails/commands/commands_tasks.rb:90:in `block in server'
    railties (5.0.0.1) lib/rails/commands/commands_tasks.rb:85:in `tap'
    railties (5.0.0.1) lib/rails/commands/commands_tasks.rb:85:in `server'
    railties (5.0.0.1) lib/rails/commands/commands_tasks.rb:49:in `run_command!'
    railties (5.0.0.1) lib/rails/commands.rb:18:in `<top (required)>'
    bin/rails:4:in `require'
    bin/rails:4:in `<main>'

Solution

  • I found the issue. In the Rails 4 days of the app, I set request.session_options to {}. It appears that it is no longer a hash in Rails 5. I now use reset_session in place of request.session_options = {}.