« return

Cannot read Rails cookies after setting them in same visit.

04-May-2009, Ant

In our situation

Setting a cookie like so

cookies[:country_code] = country_for_ip(request.remote_ip)

And then trying to read it a few lines latter like so

if cookies[:country_code] == 'US'

Did not work.  We were unable to read values until the next vistit from the browser.

I have read that you can read the values if you use a string as the key instead of a symbol like so

cookie['key']

but I opted for storing it in a regular variable for later use instead to be safe.

country_code = country_for_ip(request.remote_ip)
cookies[:country_code] = country_code

if country_code == 'US'