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

**URL:** <https://discuss.rubyonrails.org/t/fix-for-not-being-able-to-read-a-cookie-set-mid-request/44404>\
**Category:** rubyonrails-core\
**Created:** [July 20, 2009, 1:15pm UTC](https://discuss.rubyonrails.org/t/fix-for-not-being-able-to-read-a-cookie-set-mid-request/44404 "2009-07-20T13:15:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andy\_Jeffries1](https://avatars.discourse-cdn.com/v4/letter/a/cab0a1/32.png) [@Andy\_Jeffries1](https://discuss.rubyonrails.org/u/Andy_Jeffries1)\
**Post date:** [July 20, 2009, 1:15pm UTC](https://discuss.rubyonrails.org/t/fix-for-not-being-able-to-read-a-cookie-set-mid-request/44404/1 "2009-07-20T13:15:41Z")

</div>

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](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](http://www.40withegg.com/articles/rails-cookies-mangles-the-hash-interface) - not mine!).

Cheers,

Andy

---

<div class="post-metadata">

**Author:** ![Michael\_Koziarski1](https://avatars.discourse-cdn.com/v4/letter/m/aca169/32.png) [@Michael\_Koziarski1](https://discuss.rubyonrails.org/u/Michael_Koziarski1)\
**Post date:** [July 20, 2009, 10:11pm UTC](https://discuss.rubyonrails.org/t/fix-for-not-being-able-to-read-a-cookie-set-mid-request/44404/2 "2009-07-20T22:11:55Z")

</div>

> 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:
> 
> [cookies\_fix.rb · GitHub](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

---

<div class="post-metadata">

**Author:** ![Tom\_Lea](https://avatars.discourse-cdn.com/v4/letter/t/df788c/32.png) [@Tom\_Lea](https://discuss.rubyonrails.org/u/Tom_Lea)\
**Post date:** [July 20, 2009, 11:17pm UTC](https://discuss.rubyonrails.org/t/fix-for-not-being-able-to-read-a-cookie-set-mid-request/44404/3 "2009-07-20T23:17:45Z")

</div>

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] [#2768 Tests should see all cookies, not just those that are created or changed in the controller - Ruby on Rails - rails](https://rails.lighthouseapp.com/projects/8994/tickets/2768-tests-should-see-all-cookies-not-just-those-that-are-created-or-changed-in-the-controller)

---

<div class="post-metadata">

**Author:** ![Yehuda\_Katz](https://avatars.discourse-cdn.com/v4/letter/y/f14d63/32.png) [@Yehuda\_Katz](https://discuss.rubyonrails.org/u/Yehuda_Katz)\
**Post date:** [July 21, 2009, 6:49am UTC](https://discuss.rubyonrails.org/t/fix-for-not-being-able-to-read-a-cookie-set-mid-request/44404/4 "2009-07-21T06:49:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Andy\_Jeffries1](https://avatars.discourse-cdn.com/v4/letter/a/cab0a1/32.png) [@Andy\_Jeffries1](https://discuss.rubyonrails.org/u/Andy_Jeffries1)\
**Post date:** [July 21, 2009, 7:26am UTC](https://discuss.rubyonrails.org/t/fix-for-not-being-able-to-read-a-cookie-set-mid-request/44404/5 "2009-07-21T07:26:40Z")

</div>

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

Cheers,

Andy

---

<div class="post-metadata">

**Author:** ![Andy\_Jeffries1](https://avatars.discourse-cdn.com/v4/letter/a/cab0a1/32.png) [@Andy\_Jeffries1](https://discuss.rubyonrails.org/u/Andy_Jeffries1)\
**Post date:** [July 21, 2009, 7:28am UTC](https://discuss.rubyonrails.org/t/fix-for-not-being-able-to-read-a-cookie-set-mid-request/44404/6 "2009-07-21T07:28:10Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![Oliver\_Legg](https://avatars.discourse-cdn.com/v4/letter/o/a88e57/32.png) [@Oliver\_Legg](https://discuss.rubyonrails.org/u/Oliver_Legg)\
**Post date:** [July 21, 2009, 10:35am UTC](https://discuss.rubyonrails.org/t/fix-for-not-being-able-to-read-a-cookie-set-mid-request/44404/7 "2009-07-21T10:35:58Z")

</div>

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

Joshua has applied it to master.

[http://github.com/rails/rails/commit/0349278f3da9f7f532330cf295eed35ede3bae66](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](https://rails.lighthouseapp.com/projects/8994/tickets/2733-patch-memoize-cookies-so-they-can-be-retrieved-in-the-same-response-request-cycle)
