views:

519

answers:

2

It's the weirdest thing. When I ran the Rails WEBrick debugger yesterday I could do things like

cookies[uid] = s.session_id

where 'uid' is a passed argument that has a user id in it, and then expect

cookies[uid]

to give me back, say:

29b93443d9ec18168a26e1ab12957b0dd0b799ff

Today, I always get back 'nil'. I can read existing values just fine. Any of the keys listed in

cookies.keys

work just fine.

Does anyone have a possible explanation for this behavior. It is very maddening, and a Google search hasn't yielded an answer.

+2  A: 

Original questioner, here. It turns out that I can set those cookies. The issue is that when you type

cookies[:key]

in the debugger, it seems to always give you the value of the cookie as it was read from the browser. I don't know where the value gets stored away in the meantime, but inspecting the cookies in the browser confirms that my changes are indeed present. Usefully, cookies[:key] returns the value of the assignment, so you could probably do something like

@future_value = cookies[:key] = {
                                 :value => 'new value',
                                 :expires => @really_soon
                                }

I don't know what made me think I could view them before!

A: 

Also you need to read cookies using a string, rather than a symbol, since rails converts the keys to strings when you set them.

cookie[:key] = "value"
cookie[:key]  # => nil
cookie['key'] # => "value"