Cookie Memoization

11 views
Skip to first unread message

Olly Legg

unread,
May 6, 2009, 12:35:09 PM5/6/09
to Ruby on Rails: Core
Whilst trying to access cookie values that had been set in the current
request in the view, I discovered that a new CookieJar is instantiated
with every call to ActionController::Base#cookies.

Obviously in standard HTTP requests cookies can only be retrieved in
subsequent requests to the one they were set in. However the code for
CookieJar seems to suggest that the intention was to be able to access
the cookies immediately after they had been set, as CookieJar is a sub-
class of Hash.

cookies.rb: http://github.com/rails/rails/blob/7f1f16c01fd42701747daf9f3399dcdd300125f4/actionpack/lib/action_controller/cookies.rb#L72-82

Because a new CookieJar is instantiated with each access, any changes
are discarded.

What do people think the intended behavior is? We could easily keep
the changes to the cookie jar, by only instantiating one CookieJar per
request. The following code in my ApplicationController, changes the
behavior to the later: http://gist.github.com/107595

Cheers, Olly

Olly

unread,
May 28, 2009, 8:50:28 AM5/28/09
to Ruby on Rails: Core
OK, I think this change makes sense so I wiped up a patch.

+1s, improvements and objections appreciated:
https://rails.lighthouseapp.com/projects/8994/tickets/2733-patch-memoize-cookies-so-they-can-be-retrieved-in-the-same-response-request-cycle

On May 6, 5:35 pm, Olly Legg <ollyl...@gmail.com> wrote:
> Whilst trying to access cookie values that had been set in the current  
> request in the view, I discovered that a new CookieJar is instantiated  
> with every call to ActionController::Base#cookies.
>
> Obviously in standard HTTP requests cookies can only be retrieved in  
> subsequent requests to the one they were set in. However the code for  
> CookieJar seems to suggest that the intention was to be able to access  
> the cookies immediately after they had been set, as CookieJar is a sub-
> class of Hash.
>
> cookies.rb:http://github.com/rails/rails/blob/7f1f16c01fd42701747daf9f3399dcdd30...
Reply all
Reply to author
Forward
0 new messages