如何在Rails中删除通配符cookie?
时间:2020-03-05 18:50:21 来源:igfitidea点击:
如何删除使用通配符域设置的rails中的cookie:
cookies[:foo] = {:value => 'bar', :domain => '.acme.com'}
在遵循文档的情况下,我们执行以下操作:
cookies.delete :foo
日志说
Cookie set: foo=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT
请注意,该域丢失了(它似乎使用了默认值
一切的参数)。尊重RFC,当然是cookie的
仍然在那里,浏览器->ctrl
/cmd
-L
->
javascript:alert(document.cookie);
瞧!
问:删除此类Cookie的"正确"方法是什么?
解决方案
回答
在删除时也传递:domain。这是该方法的来源:
# 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
如我们所见,它只是使用我们给定的名称设置一个空Cookie,该Cookie将于1969年到期,并且没有任何内容。但它会合并我们提供的任何其他选项,因此我们可以执行以下操作:
cookies.delete :foo, :domain => '.acme.com'
而且我们已设定。