Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For some reason replacing <code>:all</code> with the domain did not work (rails 3.2.11) for me. It took a piece of custom Middleware to fix it. A summary of that solution is below.</p> <p><strong>tl;dr:</strong> You need to write a custom Rack Middleware. You need add it into your <code>conifg/environments/[production|development].rb</code>. This is on Rails 3.2.11</p> <p>Cookie sessions are usually stored only for your top level domain.</p> <p>If you look in <code>Chrome -&gt; Settings -&gt; Show advanced settings… -&gt; Privacy/Content settings… -&gt; All cookies and site data… -&gt; Search {yourdomain.com}</code> You can see that there will be separate entries for <code>sub1.yourdomain.com</code> and <code>othersub.yourdomain.com</code> and <code>yourdomain.com</code></p> <p>The challenge is to use the same session store file across all subdomains.</p> <h2>Step 1: Add Custom Middleware Class</h2> <p>This is where <a href="http://guides.rubyonrails.org/rails_on_rack.html" rel="nofollow noreferrer">Rack Middleware</a> comes in. Some relevant rack &amp; rails resources:</p> <ul> <li><a href="http://railscasts.com/episodes/151-rack-middleware?view=asciicast" rel="nofollow noreferrer">Railscasts about Rack</a></li> <li><a href="http://guides.rubyonrails.org/rails_on_rack.html" rel="nofollow noreferrer">Railsguide for Rack</a></li> <li>Rack documentation for <a href="http://rack.rubyforge.org/doc/Rack/Session/Abstract/ID.html" rel="nofollow noreferrer">sesssions abstractly</a> and for <a href="http://rack.rubyforge.org/doc/Rack/Session/Cookie.html" rel="nofollow noreferrer">cookie sessions</a></li> </ul> <p>Here is a custom class that you should add in the <code>lib</code> <strong>This was written by <a href="https://stackoverflow.com/users/166918/nader">@Nader</a> and you all should thank him</strong></p> <pre><code># Custom Domain Cookie # # Set the cookie domain to the custom domain if it's present class CustomDomainCookie def initialize(app, default_domain) @app = app @default_domain = default_domain end def call(env) host = env["HTTP_HOST"].split(':').first env["rack.session.options"][:domain] = custom_domain?(host) ? ".#{host}" : "#{@default_domain}" @app.call(env) end def custom_domain?(host) host !~ /#{@default_domain.sub(/^\./, '')}/i end end </code></pre> <p>Basically what this does is that it will map all of your cookie session data back onto the exact same cookie file that is equal to your root domain.</p> <h2>Step 2: Add To Rails Config</h2> <p>Now that you have a custom class in lib, make sure are autoloading it. If that meant nothing to you, look here: <a href="https://stackoverflow.com/questions/4073856/rails-3-autoload">Rails 3 autoload</a></p> <p>The first thing is to make sure that you are system-wide using a cookie store. In <strong><code>config/application.rb</code></strong> we tell Rails to use a cookie store.</p> <pre><code># We use a cookie_store for session data config.session_store :cookie_store, :key =&gt; '_yourappsession', :domain =&gt; :all </code></pre> <p>The reason this is here is mentioned here is because of the <code>:domain =&gt; :all</code> line. There are other people that have suggested to specify <code>:domain =&gt; ".yourdomain.com"</code> instead of <code>:domain =&gt; :all</code>. For some reason this did not work for me and I needed the custom Middleware class as described above.</p> <p>Then in your <strong><code>config/environments/production.rb</code></strong> add:</p> <pre><code>config.middleware.use "CustomDomainCookie", ".yourdomain.com" </code></pre> <p>Note that the preceding dot is necessary. See "<a href="https://stackoverflow.com/questions/3865345/sub-domain-cookies-sent-in-a-parent-domain-request">sub-domain cookies, sent in a parent domain request?</a>" for why.</p> <p>Then in your <strong><code>config/environments/development.rb</code></strong> add:</p> <pre><code>config.middleware.use "CustomDomainCookie", ".lvh.me" </code></pre> <p>The lvh.me trick maps onto localhost. It's awesome. See <a href="http://railscasts.com/episodes/221-subdomains-in-rails-3" rel="nofollow noreferrer">this Railscast about subdomains</a> and <a href="http://pastebin.com/GUrn7vvE" rel="nofollow noreferrer">this note</a> for more info.</p> <p>Hopefully that should do it. I honestly am not entirely sure why the process is this convoluted, as I feel cross subdomain sites are common. If anyone has any further insights into the reasons behind each of these steps, please enlighten us in the comments.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload