在Rails中的子域之间共享会话(cookies)? [英] Share session (cookies) between subdomains in Rails?

查看:160
本文介绍了在Rails中的子域之间共享会话(cookies)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序设置,每个用户属于一个公司,该公司有一个子域名(我正在使用basecamp风格的子域名)。我正在面临的问题是,rails正在创建多个cookie(一个用于lvh.me,另一个用于subdomain.lvh.me),这在我的应用程序中导致了相当多的中断(例如,Flash消息持续存在,但出现所有请求一次

I have an app setup where each user belongs to a company, and that company has a subdomain (I am using basecamp style subdomains). The problem that I am facing is that rails is creating multiple cookies (one for lvh.me and another for subdomain.lvh.me) which is causing quite a few breaks in my application(such as flash messages being persistent though out all requests once signed in).

我在我的/cofig/initilizers/session_store.rb文件中有:

I have this in my /cofig/initilizers/session_store.rb file:

AppName::Application.config.session_store :cookie_store, key: '_application_devise_session', domain: :all

域名::似乎都是Google发现的标准答案,但这似乎并不适合我。任何帮助是赞赏!

The domain: :all seems to be the standard answer I found on Google, but that doesn't seem to be working for me. Any help is appreciated!

推荐答案

因为它变成域:全部为在该会话期间访问的所有不同的子域创建一个cookie(和它确保它们在请求之间传递)。如果没有传递域参数,则意味着为同一会话中访问的每个不同的域创建一个新的cookie,而旧的被丢弃。我需要的是一个在整个会话中持久的单个cookie,即使域更改。因此,通过 domain:lvh.me 解决了开发中的问题。这将创建一个单一的cookie,它们位于不同子域之间。

As it turns outs 'domain: all' creates a cookie for all the different subdomains that are visited during that session (and it ensures that they are passed around between request). If no domain argument is passed, it means that a new cookie is created for every different domain that is visited in the same session and the old one gets discarded. What I needed was a single cookie that is persistent throughout the session, even when the domain changes. Hence, passing domain: lvh.me solved the problem in development. This creates a single cookie that stays there between different subdomains.

对于需要进一步解释的任何人来说,这是一个很棒的链接:
http://excid3.com/blog/sharing-a-devise -user-session-across-subdomains-with-rails-3 /

For anyone needing further explanation, this is a great link: http://excid3.com/blog/sharing-a-devise-user-session-across-subdomains-with-rails-3/

这篇关于在Rails中的子域之间共享会话(cookies)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆