开发者

Sinatra - response.set_cookie doesn't work

开发者 https://www.devze.com 2023-02-12 08:15 出处:网络
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\')

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})

d开发者_StackOverflowoes not work. No cookie is made. I need this so much....

Please help... thanks!


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"]

SUMMARY

don't set localhost as a domain for your cookies because you need to set it to "" or FALSE

0

精彩评论

暂无评论...
验证码 换一张
取 消