Deleting Cookies from a Controller Deleting Cookies from a Controller ruby-on-rails ruby-on-rails

Deleting Cookies from a Controller


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


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'


We can delete the cookies by passing name and options to delete method as follows:

Syntax: delete(name, options = {})

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.

Example:

cookies.delete('JWT', {   value: "",    expires: 1.day.ago,   path: '/api'})