2008-09-09 8 views
13

:Rails에서 와일드 카드 쿠키를 어떻게 삭제합니까? 당신이 와일드 카드 도메인을 설정 레일에 쿠키를 삭제하려면 어떻게

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

때, docs 다음, 당신이 할 :

cookies.delete :foo 

는 로그

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

도메인이 누락되었습니다 (모든 항목에 기본 매개 변수가 사용 된 것 같습니다). 은 RFC를 존중, 물론 쿠키의 여전히, 브라우저 ->Ctrl 키를/cmd를 - L ->

javascript:alert(document.cookie); 

자보세요!

Q : 그런 쿠키를 삭제하는 "올바른"방법은 무엇입니까?

+0

Thx! 그것은 매력처럼 작동합니다 ... 또한 http://api.rubyonrails.org/classes/ActionController/Cookies.html "docs"링크를 추가하려고했지만 실패했을 수 있습니다. – Purfideas

답변

20

삭제시 도메인을 전달하십시오. 여기에 그 방법의 소스는 다음과 같습니다

# 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 

당신이 볼 수 있듯이, 그것은 단지 1969 년에 만료되도록 설정하면 지정한 이름을 가진 빈 쿠키를 설정하고, 어떤 내용으로. 하지만 당신이 줄 수있는 다른 옵션을 병합, 그래서 할 수 있습니다 :

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

그리고 당신이 설정되었습니다.