# Issue in delete a cookie ( cannot 'eat' it.)

**URL:** <https://www.ruby-forum.com/t/issue-in-delete-a-cookie-cannot-eat-it/224454>\
**Category:** Rails\
**Created:** [November 12, 2012, 6:48pm UTC](https://www.ruby-forum.com/t/issue-in-delete-a-cookie-cannot-eat-it/224454 "2012-11-12T18:48:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [November 12, 2012, 6:48pm UTC](https://www.ruby-forum.com/t/issue-in-delete-a-cookie-cannot-eat-it/224454/1 "2012-11-12T18:48:40Z")

</div>

I set my cookies at root domain level ( [lvh.me](http://lvh.me) in dev) , accessible  
from  
within each subdomain  
w session store initializer :  
Rails.application.config.session\_store :active\_record\_store, :key =\>  
‘\_tests\_session’, :domain =\> :all  
( good for persisted facebook authentication when user login into a  
different subdomain)

when a user login into a subdomain, I have  
cookies[:login]  
“{:user\_id=\>3, :subdomain\_name=\>"coocoo"}”

when the user logout , I should delete this cookie,  
cookies.delete(:login, :host =\> Rails.configuration.host) if  
cookies[:login]

but it doesn’t delete it … any clue ?  
thanks for feedback

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [November 13, 2012, 5:24am UTC](https://www.ruby-forum.com/t/issue-in-delete-a-cookie-cannot-eat-it/224454/2 "2012-11-13T05:24:39Z")

</div>

On Mon, Nov 12, 2012 at 11:47 AM, Erwin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I set my cookies at root domain level ( [lvh.me](http://lvh.me) in dev) , accessible from  
> within each subdomain  
> w session store initializer :  
> Rails.application.config.session\_store :active\_record\_store, :key =\>  
> ‘\_tests\_session’, :domain =\> :all

Did you try setting your domain to the actual domain?

Rails.application.config.session\_store :active\_record\_store, :key =\>  
‘\_tests\_session’, :domain =\> ‘[example.com](http://example.com)’

> when a user login into a subdomain, I have  
> cookies[:login]  
> “{:user\_id=\>3, :subdomain\_name=\>"coocoo"}”
> 
> when the user logout , I should delete this cookie,  
> cookies.delete(:login, :host =\> Rails.configuration.host) if cookies[:login]
> 
> but it doesn’t delete it … any clue ?

–  
Greg D.

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [November 13, 2012, 7:47am UTC](https://www.ruby-forum.com/t/issue-in-delete-a-cookie-cannot-eat-it/224454/3 "2012-11-13T07:47:46Z")

</div>

It’s not useful in my case , as I am using FB & Google  
authentification,  
the callback can be set only to the main domain  
using the :state parameter I can redirect to the calling subdomain on  
FB/Google callback  
but before the redirect I need to delete the previous session/cookies (  
user may have been logged in an another subdomain or root domain)

setting :domain =\> :all , means that the cookies :host is/should\_be  
‘.example.com’  
and I should be able to delete it : ( as per Rails API)

cookies.delete(:login, :host =\> ‘.example.com’) but it’s not working

I am going to test another way : expiring the cookie,…

def remove\_all\_cookies(domain=nil)  
unless domain.nil?  
cookies.to\_hash.each\_pair { |k, v| cookies[k.to\_sym] = { :value  
=\>  
‘’, :path =\> ‘/’, :domain =\> domain, :expire =\> 1.day.ago } }  
end  
end

Le mardi 13 novembre 2012 05:24:02 UTC+1, Greg D. a crit :

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [November 13, 2012, 12:02pm UTC](https://www.ruby-forum.com/t/issue-in-delete-a-cookie-cannot-eat-it/224454/4 "2012-11-13T12:02:00Z")

</div>

[SOLVED] the issue was not with cookies … I had to change the way I  
handle the session store w subdomain … should not using domain =\>  
:all

I finally set in initializers/session\_store.rb

Rails.application.config.session\_store :active\_record\_store, :key =\>  
‘\_myapp\_session’, :domain =\> {  
production: ‘[myapp.com](http://myapp.com)’,  
development: ‘[lvh.me](http://lvh.me)’}.fetch(Rails.env.to\_param.to\_sym, :all)

so now I can set/delete the cookies[:login] according to user  
authentication w FB/Google and originated subdomain ( parameter :state  
in  
URL)

on signout, I have to delete the cookies[:login], on redirection I have  
to  
check if the cookies[:login] is related to the subdomain else delete  
it…

great post and link about it :

Quoted- 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](http://lvh.me)’  
solved the problem in development. This creates a single cookie that  
stays  
there between different subdomains.

For anyone needing further explanation,  
this is a great link:

> **[Sharing A Devise User Session Across Subdomains With Rails 3](https://excid3.com/blog/sharing-a-devise-user-session-across-subdomains-with-rails-3/)**
>
> Recently I’ve been working on a Rails application that supports subdomains. I’m using Devise for user authentication and need the user to choose a subdomain ...

Le mardi 13 novembre 2012 07:46:37 UTC+1, Erwin a crit :
