Sinatra - response.set_cookie 不起作用 [英] Sinatra - response.set_cookie doesn't work
问题描述
我需要为我的 Sinatra 应用程序使用 cookie.如果我使用最简单的方法是可行的:
I need to use a cookie for my Sinatra application. If I use the simpliest method is works:
response.set_cookie('my_cookie', 'value_of_cookie')
但我需要一些选项,例如域和过期日期,所以我试试这个:
but I need some options such as domain and expire date so I try this:
response.set_cookie("my_cookie", {:value => 'value_of_cookie', :domain => myDomain, :path => myPath, :expires => Date.new})
不起作用.没有饼干.我非常需要这个......
does not work. No cookie is made. I need this so much....
请帮忙...谢谢!
推荐答案
http://上的文档sinatra-book.gittr.com/#cookies 说要使用 set_cookie 帮助程序,但在较新版本的 Sinatra(至少从 1.2.0+ 起可能更早)中,您应该使用 response.set_cookie 来设置 cookie.
The documentation on http://sinatra-book.gittr.com/#cookies says to use the set_cookie helper, but in newer versions of Sinatra (at least from 1.2.0+ and possibly earlier), you should use response.set_cookie to set cookies.
response.set_cookie("my_cookie", :value => "value_of_cookie",
:domain => myDomain,
:path => myPath,
:expires => Date.new(2020,1,1))
cookie = request.cookies["my_cookie"]
摘要
不要将 localhost
设置为 cookie 的域,因为您需要将其设置为 "" 或 FALSE
don't set localhost
as a domain for your cookies because you need to set it to "" or FALSE
这篇关于Sinatra - response.set_cookie 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!