Sinatra - response.set_cookie doesn't work

依然范特西╮ 提交于 2020-01-20 18:50:20

问题


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....

Please help... thanks!


回答1:


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



来源:https://stackoverflow.com/questions/5078091/sinatra-response-set-cookie-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!