andyjeffries
2009-Jul-20 13:15 UTC
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
Michael Koziarski
2009-Jul-20 22:11 UTC
Re: Fix for not being able to read a cookie set mid-request
> 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/150330Can 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 -- Cheers Koz
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 On 20 Jul 2009, at 23:11, Michael Koziarski wrote:> >> 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 > > > > > -- > Cheers > > Koz > > >
Yehuda Katz
2009-Jul-21 06:49 UTC
Re: Fix for not being able to read a cookie set mid-request
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 On Mon, Jul 20, 2009 at 3:11 PM, Michael Koziarski <michael@koziarski.com>wrote:> > > 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 > > > > > -- > Cheers > > Koz > > > >-- Yehuda Katz Developer | Engine Yard (ph) 718.877.1325 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
andyjeffries
2009-Jul-21 07:26 UTC
Re: Fix for not being able to read a cookie set mid-request
On Jul 20, 11:11 pm, Michael Koziarski <mich...@koziarski.com> wrote:> > 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: > > 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.4Sure, it''s now ticket #2930 and it''s assigned to you. Cheers, Andy
andyjeffries
2009-Jul-21 07:28 UTC
Re: Fix for not being able to read a cookie set mid-request
> 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
Olly Legg
2009-Jul-21 10:35 UTC
Re: Fix for not being able to read a cookie set mid-request
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 On 20 Jul 2009, at 14:15, andyjeffries wrote:> > > 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 > >