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})
无效。没有cookie。我需要这么多....
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屋!