从Rails 3.2升级到Rails 4.1.4后,使用现有会话访问应用程序(从较旧的Rails 3.2版本)导致内部服务器错误.回溯:
JSON::ParserError - 795: unexpected token at { I"session_id:ETI"%fa78a4ee07ac952c9b034ebc6199f30b;': /Users/.../.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/json/common.rb:155:in `parse' actionpack (4.1.4) lib/action_dispatch/middleware/cookies.rb:388:in `load' actionpack (4.1.4) lib/action_dispatch/middleware/cookies.rb:428:in `deserialize' actionpack (4.1.4) lib/action_dispatch/middleware/cookies.rb:183:in `verify_and_upgrade_legacy_signed_message' actionpack (4.1.4) lib/action_dispatch/middleware/cookies.rb:550:in `[]' actionpack (4.1.4) lib/action_dispatch/middleware/session/cookie_store.rb:114:in `get_cookie' actionpack (4.1.4) lib/action_dispatch/middleware/session/cookie_store.rb:90:in `block in unpacked_cookie_data' actionpack (4.1.4) lib/action_dispatch/middleware/session/abstract_store.rb:51:in `stale_session_check!' actionpack (4.1.4) lib/action_dispatch/middleware/session/cookie_store.rb:89:in `unpacked_cookie_data' actionpack (4.1.4) lib/action_dispatch/middleware/session/cookie_store.rb:83:in `block in extract_session_id' actionpack (4.1.4) lib/action_dispatch/middleware/session/abstract_store.rb:51:in `stale_session_check!' actionpack (4.1.4) lib/action_dispatch/middleware/session/cookie_store.rb:82:in `extract_session_id' actionpack (4.1.4) lib/action_dispatch/request/session.rb:49:in `block in []' actionpack (4.1.4) lib/action_dispatch/request/session.rb:48:in `[]' actionpack (4.1.4) lib/action_dispatch/request/session.rb:70:in `id' rack (1.5.2) lib/rack/session/abstract/id.rb:282:in `current_session_id' rack (1.5.2) lib/rack/session/abstract/id.rb:288:in `session_exists?' actionpack (4.1.4) lib/action_dispatch/request/session.rb:152:in `exists?' actionpack (4.1.4) lib/action_dispatch/request/session.rb:172:in `load_for_read!' actionpack (4.1.4) lib/action_dispatch/request/session.rb:89:in `[]' warden (1.2.3) lib/warden/session_serializer.rb:30:in `fetch' warden (1.2.3) lib/warden/proxy.rb:212:in `user' warden (1.2.3) lib/warden/proxy.rb:318:in `_perform_authentication' warden (1.2.3) lib/warden/proxy.rb:104:in `authenticate' warden (1.2.3) lib/warden/proxy.rb:114:in `authenticate?' devise (3.2.4) lib/devise/rails/routes.rb:460:in `block in constraints_for' actionpack (4.1.4) lib/action_dispatch/routing/mapper.rb:38:in `block in matches?' actionpack (4.1.4) lib/action_dispatch/routing/mapper.rb:36:in `matches?' actionpack (4.1.4) lib/action_dispatch/routing/mapper.rb:45:in `call' actionpack (4.1.4) lib/action_dispatch/journey/router.rb:71:in `block in call' actionpack (4.1.4) lib/action_dispatch/journey/router.rb:59:in `call' actionpack (4.1.4) lib/action_dispatch/routing/route_set.rb:678:in `call' ...
我尝试更改会话cookie密钥名称,但似乎被阻塞在session_id上.
# initializers/session_store.rb MyApp::Application.config.session_store :cookie_store,key: 'myapp_session'
解决方法
在这里找到答案:
https://github.com/rails/rails/issues/15111
我的设置有
# initializers/cookie_serializer.rb Rails.application.config.action_dispatch.cookies_serializer = :json
我改变了
Rails.application.config.action_dispatch.cookies_serializer = :hybrid
那就是诀窍