升级到 Rails 5 时出现新错误:NoMethodError({} 的未定义方法“id”:哈希)

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

我最近升级到 Rails 5。升级并修复了常见问题后,我的应用程序似乎 运行 正确并提供响应。

尽管我的测试套件通过了 94% 的覆盖率,但在执行实际请求时,控制器在 后崩溃,声称该请求是 200 OK

看起来堆栈跟踪并未指向我的应用程序中的任何代码,而是指向 Actionpack 内部的某些内容。

这是我的错误配置,还是 Rails 5 中的合法错误?

在 141 毫秒内完成 200 OK(视图:2.2 毫秒 | ActiveRecord:124.9 毫秒)

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

我发现了问题。在 Rails 4 天的应用中,我 set request.session_options to {}。看来它不再是 Rails 中的散列 5. 我现在使用 reset_session 代替 request.session_options = {}