Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting Cookies from a Controller

I set some cookie values in my form using jQuery. I can read them just fine in my Rails controller via the cookies method. When I call cookies.delete(:my_key), they appear to be gone when I call cookies again. But when I reload the page, the cookies are back again.

Is there a way to delete the cookies for good from inside my controller?

EDIT

This is very strange since I'm looking at the response headers and they seem to be deleting the cookie. Maybe it's because it is a 302 request?

Set-Cookie: my_key=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT 
like image 849
Dex Avatar asked May 23 '11 23:05

Dex


People also ask

Is it a good idea to remove all cookies?

If you're using a public computer, you should delete them and other data, such as browsing history, right after your session. If it's your personal device, it's a good idea to remove all cookies at least once a month to keep your device neat.

Can you permanently remove cookies?

On your device, launch the Chrome app. At the top right, tap on the three dots, scroll down, and then select “Settings.” Under settings, tap “Privacy” then “Clear browsing data.” Select “Cookies, site data” and uncheck all other items.

How do I delete cookies 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.


2 Answers

For example, your cookie look like this

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

As you tried this one => cookies.delete :foo

The logs will say => Cookie set: foo=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT

Notice that the domain is missing. Tried this way

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

Function = >

# 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 
like image 194
Rubyist Avatar answered Oct 05 '22 20:10

Rubyist


According to the rails api, there is now a delete method, so if you have not set the domain use

cookies.delete :my_key 

and if you have set the domain

cookies.delete :my_key, domain: 'mydomain.com' 
like image 44
Obromios Avatar answered Oct 05 '22 22:10

Obromios