Rails 会话 ID 为零

发布于 2024-12-09 17:40:58 字数 478 浏览 1 评论 0原文

我有一个 Rails 2.3.5 应用程序,我正在将其迁移到 Rails 3.0.10,但遇到了会话问题。

某些代码在集成测试期间失败,因为会话没有 id(request.session 是一个空哈希,request.session_options[:id] 为 nil)。我们目前使用的是 webrat,但同样的事情也发生在 capybara 和标准的 Rails 助手上。会话似乎确实设置正确,因为某些集成测试具有会话 ID。看起来会话 ID 只在尝试访问会话 ID 之前未登录的测试中丢失(即大多数测试)。

我尝试将会话后端(当前基于 cookie)切换到 ActiveRecord,但这没有帮助。我创建了一个新的 Rails 3.0.10 应用程序,它也有 nil 会话 ID。我什至深入研究了 Rails 的会话处理,看起来如果会话缺少会话 ID,则不会生成会话 ID。

缺少会话 ID 的预期行为是否记录在某处?如果这是一个错误,是否有一个 Rails 版本可以正确生成会话 ID?

I've got a Rails 2.3.5 application that I'm migrating to Rails 3.0.10 that's run into a problem with sessions.

Some code is failing during integration tests because the session has no id (request.session is an empty hash, and request.session_options[:id] is nil). We are currently using webrat, but the same thing happens with capybara and with standard rails helpers. Sessions do seem to be set up properly, since some of the integration tests have session ids. It looks like the session id is missing only in tests that don't log in before trying to access the session id (i.e. most of them).

I've tried switching the session backend (it's currently cookie based) to ActiveRecord, but that didn't help. I've created a new Rails 3.0.10 app, and it also has nil session ids. I've even dug into Rails's session handling, and it looks like there's no session id being generated on if a session is missing one.

Is this lack of session ids expected behavior that's documented somewhere? If it's a bug, is there a version of Rails that generates session ids properly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疯狂的代价 2024-12-16 17:40:58

我见过的解决这个问题的唯一方法就是准备会议。

sid1 = session[:session_id]      
sid2 = request.session_options[:id]

但是,如果由于某种原因会话 cookie 被删除,这种情况仍然可能发生。

The only way I've seen how to solve this is to prime the sessions.

sid1 = session[:session_id]      
sid2 = request.session_options[:id]

However, if for some reason the session cookie is deleted, it can still happen.

看轻我的陪伴 2024-12-16 17:40:58

我尝试过,但没有结果。我还尝试了 session[:foo] = 'bar' 因为我喜欢它看起来更好的方式,但仍然没有结果。

我重新启动,仍然没有喜悦。然后我进入浏览器设置并删除了我的cookie。这解决了它。从这次经验来看,cookie 似乎可以防止会话被强制。我一直在更改类名并更改我的数据库表。不知怎的,我损坏了我的饼干。

I tried that and no result. I also tried session[:foo] = 'bar' as I liked the way it looked better but still no result.

I rebooted and still no joy. Then I went into my browser settings and deleted my cookies. That fixed it. From this experience it appears a cookie can keep sessions from being forced. I had been changing class names and changing my database tables. Somehow I had corrupted my cookie.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文