Errors like "NoMethodError: undefined` sweep 'method for # <Hash ... "after switching to Rails 3 from Rails 4
We upgraded to Rails 4, had some major problems, and downgraded again (returned a commit commit).
Then we got errors like
NoMethodError (undefined method `sweep' for #<Hash:0x007f01ab44a940>): seems to be because Rails 4 stores flash memory in a session in such a way that Rails 3 cannot read.
What is a good way to solve this problem?
+6
2 answers
We decided to solve this by fixing Rails ourselves to catch this error and remove the borked flash. This means that it heals itself transparently enough.
We also included this patch only in Rails 3, so it does not cause problems when we make another attempt to upgrade to Rails 4.
We are stuck in config/initializers/rails4_to_rails3_downgradability.rb :
if Rails::VERSION::MAJOR == 3 module ActionDispatch class Flash def call(env) if (session = env['rack.session']) && (flash = session['flash']) # Beginning of change! if flash.respond_to?(:sweep) flash.sweep else session.delete("flash") end # End of change! end @app.call(env) ensure session = env['rack.session'] || {} flash_hash = env[KEY] if flash_hash if !flash_hash.empty? || session.key?('flash') session["flash"] = flash_hash new_hash = flash_hash.dup else new_hash = flash_hash end env[KEY] = new_hash end if session.key?('flash') && session['flash'].empty? session.delete('flash') end end end end end +8