Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sinatra - response.set_cookie doesn't work

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!

like image 987
claudiut Avatar asked Feb 22 '11 12:02

claudiut


1 Answers

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

like image 55
fl00r Avatar answered Nov 20 '22 03:11

fl00r