Rails 4 FlashHash Upgrade Gotcha

We upgraded an application from Rails 3 to Rails 4 this week and came across an interesting gotcha which I haven’t seen documented anywhere.

As we rolled out the Rails 4 version of the app we split the traffic between the upgraded Rails 4 app and our existing Rails 3 app. Some of the requests to Rails 3 app failed with the following exception:

Production NoMethodError: undefined method `sweep' for {"discard"=>[], "flashes"=>{"just_switched"=>true}}:Hash

Error Message: NoMethodError: undefined method `sweep' for {"discard"=>[], "flashes"=>{"just_switched"=>true}}:Hash

Where:
[GEM_ROOT]/gems/actionpack-3.2.19/lib/action_dispatch/middleware/flash.rb, line 239

It turns out that Rails 4 serialise the flash to the cookie differently to Rails 3. When Rails 3 attempts to deserialise it you get the above error. This error can also occur between Rails 3 and Sinatra apps.

To get around the problem whilst in the migration period we patched the Hash class. This would mean of course that the flash methods wouldn’t work, but as they are only used for minor presentation tweaks that seemed a good compromise.


class Hash
def now
Rails.logger.warn "Stubbing now during upgrade"
{}
end
def keep
# stub keep for upgrade purposes
Rails.logger.warn "Stubbing keep during upgrade"
end
def sweep
# stub sweep for upgrade purposes
Rails.logger.warn "Stubbing sweep during upgrade"
end
end

view raw

hash.rb

hosted with ❤ by GitHub

5 comments

  1. Muntasir Muneer · April 15, 2015

    Thanks for sharing. Did you try clearing your browser cache and cookie? It happened to me as well and started working again after cache and cookies got reset.

  2. avnerner · April 16, 2015

    The point is not my cookies or cache, it’s the users of my site. I can’t ask 3M users to delete cookies..

  3. Henrik Nyh · June 18, 2015

    This is how we fixed (I hope – only verified to work in dev so far) the sweep errors when downgrading from a botched Rails 4 upgrade to Rails 3: https://gist.github.com/henrik/bb6732d5d4cddb5085a4

  4. Henrik N · June 18, 2015

    This is how we fixed (I hope – only verified to work in dev so far) the sweep errors when downgrading from a botched Rails 4 upgrade to Rails 3: https://gist.github.com/henrik/bb6732d5d4cddb5085a4

Leave a comment