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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Check out – https://rubygems.org/gems/rails_4_session_flash_backport
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.
The point is not my cookies or cache, it’s the users of my site. I can’t ask 3M users to delete cookies..
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
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