Fix for not being able to read a cookie set mid-request

Hi guys,

Doing this within an action won't return the expected result:

cookies[:abc] = "123"
render :text => cookies[:abc]

The reason has nothing to do with strings/symbols as cookie keys but
is due to the fact that ActionController's cookies method gets a fresh
CookieJar every time it's called.

The fix (monkeypatched here in an initializer) is simply:

http://gist.github.com/150330

I even note that the []= method seems to call super as if this
functionality is expected to work.

I know everyone's probably focussed on 3.0, but this is a real doozy
of an issue if you hit it (for example, see the blog post at -
http://www.40withegg.com/articles/rails-cookies-mangles-the-hash-interface
- not mine!).

Cheers,

Andy

The reason has nothing to do with strings/symbols as cookie keys but
is due to the fact that ActionController's cookies method gets a fresh
CookieJar every time it's called.

The fix (monkeypatched here in an initializer) is simply:

http://gist.github.com/150330

Can you open a lighthouse ticket for this and assign it to me please?
I'll be looking after 2-3-stable until 3.0 ships, so I'll be sure to
have it wrapped up for 2.3.4

In other cookie related news, [1] left us with false negatives.

Basically in a test case, all cookies are effectively reset, so
testing a cookie delete always passes.

[1] https://rails.lighthouseapp.com/projects/8994/tickets/2768-tests-should-see-all-cookies-not-just-those-that-are-created-or-changed-in-the-controller

Koz,

Once you’re done with it, can you reassign it to 3.0. There’s a chance this bug still exists in 3.0, since some of the implementation of that stuff is still the same at the moment.

– Yehuda

Sure, it's now ticket #2930 and it's assigned to you.

Cheers,

Andy

Once you're done with it, can you reassign it to 3.0. There's a chance this
bug still exists in 3.0, since some of the implementation of that stuff is
still the same at the moment.

Hi Yehuda,

I did have a look around yesterday at the 3.0 source to see if the
problem existed, but things seem to have moved around considerably so
finding the file wasn't as easy and we were more concerned about
fixing it for our app which uses 2.3.2.

I've created the ticket though and assigned it to Koz, so I guess it
will come your way soon.

Cheers,

Andy

I came across the exact same problem and submitted a patch.

Joshua has applied it to master.

http://github.com/rails/rails/commit/0349278f3da9f7f532330cf295eed35ede3bae66
https://rails.lighthouseapp.com/projects/8994/tickets/2733-patch-memoize-cookies-so-they-can-be-retrieved-in-the-same-response-request-cycle