0

我正在尝试删除表单字段值的 cookie。Rails text_field 助手将 [] 添加到名称中。即名称=用户[名称]

在这种情况下,cookie 是使用路径创建的。(路径='/登录/')

当我尝试删除 cookie

cookies.delete("user[name]")

[] 通过 Rack 获取 URL 编码。

我可以像这样将cookie设置为nil:

response['set-cookie']='user[name]='

这会清除 cookie,但仅适用于路径“/login”。(没有尾部斜杠)需要尾部斜杠以避免 IE8 错误,即在没有尾部斜杠的情况下无法存储 cookie。

如何直接从响应对象设置cookie并同时设置路径?

4

1 回答 1

0

我能够通过以下操作完成删除 cookie:

response["Set-Cookie"] = 'user[name]=; path=/login/; expires=Thu, 01-Jan-1970 00:00:00 GMT'

要删除第二个 cookie,您可以执行以下操作:

response["Set-Cookie"] = [ response["Set-Cookie"], 'user[email]=; path=/login/; expires=Thu, 01-Jan-1970 00:00:00 GMT' ].join("\n")

这正是 Rack (v1.1.3) 的做法。

然后,确认第一个 cookie 已删除的规范如下所示:

it "should delete the cookie used to store the username form field" do
  post :login, @correct_form_params
  response["Set-Cookie"][0].should =~ /user\[username\]=;/
end

希望这可以帮助其他人!

于 2013-07-19T22:13:02.113 回答