Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you delete wild card cookies in Rails?

How do you delete a cookie in rails that was set with a wild card domain:

cookies[:foo] = {:value => 'bar', :domain => '.acme.com'}

When, following the docs, you do:

cookies.delete :foo

the logs say

Cookie set: foo=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT

Notice that the domain is missing (it seems to use the default params for everything). Respecting the RFC, of course the cookie's still there, Browser -> ctrl/cmd-L ->

javascript:alert(document.cookie);

Voilà!

Q: What's the "correct" way to delete such a cookie?

like image 988
Purfideas Avatar asked Sep 09 '08 21:09

Purfideas


People also ask

How do I delete a cookie in rails?

Description: Removes the cookie on the client machine by setting the value to an empty string and the expiration date in the past. Like []=, you can pass in an options hash to delete cookies with extra data such as a :path.

How do cookies work in Rails?

Cookies are read and written through ActionController#cookies. The cookies being read are the ones received along with the request, the cookies being written will be sent out with the response. Reading a cookie does not get the cookie object itself back, just the value it holds.


1 Answers

Pass the :domain on delete as well. Here's the source of that method:

# Removes the cookie on the client machine by setting the value to an empty string
# and setting its expiration date into the past.  Like []=, you can pass in an options
# hash to delete cookies with extra data such as a +path+.
def delete(name, options = {})
  options.stringify_keys!
  set_cookie(options.merge("name" => name.to_s, "value" => "", "expires" => Time.at(0)))
end

As you can see, it just sets an empty cookie with the name you gave, set to expire in 1969, and with no contents. But it does merge in any other options you give, so you can do:

cookies.delete :foo, :domain => '.acme.com'

And you're set.

like image 180
Jordi Bunster Avatar answered Sep 19 '22 06:09

Jordi Bunster