Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

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

#2930

Koz asked me to post this here:

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:

module ActionController #:nodoc:
module Cookies

def cookies
  @cookies ||= CookieJar.new(self)
end

end end

The current code in actionpack/lib/action_controller/cookies.rb looks like this:

def cookies
  CookieJar.new(self)
end

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-in... - not mine!).

Reported by andyjeffries · July 21st, 2009 @ 08:25 AM

State: resolved
Milestone: 2.x
Assigned to: Michael Koziarski Michael Koziarski
Importance: none

Activity

  1. andyjeffries
    andyjeffries
    • Tag changed from action_controller, cookies to action_controller, cookies, patch

    July 21st, 2009 @ 08:25 AM

  2. andyjeffries
    andyjeffries

    The original gist (I was trying to be helpful in pasting the whole code here) shows it better as lighthouse appears to have mangled the formatting:

    http://gist.github.com/150330

    July 21st, 2009 @ 08:30 AM

  3. Michael Koziarski
    Michael Koziarski
    • State changed from new to resolved

    This was a dupe of #2733 [PATCH] Memoize cookies so they can be retrieved in the same response / reque... and I've cherry-picked that patch over.

    July 21st, 2009 @ 10:46 PM