Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Fix "NoMethodError: undefined method `sweep'" error when downgrading from Rails 4 to Rails 3.
# Without this fix, downgrading from Rails 4 to Rails 3 causes session cookies to blow up.
#
# The way the flash is stored in the session changed in a backwards-incompatible way.
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
@henrik

This comment has been minimized.

Show comment
Hide comment
@henrik

henrik Jun 18, 2015

@victorarias came up with the idea for this fix.

Verified to work in dev; not yet in prod.

Owner

henrik commented Jun 18, 2015

@victorarias came up with the idea for this fix.

Verified to work in dev; not yet in prod.

@jivebot

This comment has been minimized.

Show comment
Hide comment
@jivebot

jivebot Sep 4, 2015

Thanks for sharing this! This eases my concerns in possibly having to roll back from Rails 4 to Rails 3.

jivebot commented Sep 4, 2015

Thanks for sharing this! This eases my concerns in possibly having to roll back from Rails 4 to Rails 3.

@buffym

This comment has been minimized.

Show comment
Hide comment
@buffym

buffym Nov 25, 2015

Thank you for sharing!

buffym commented Nov 25, 2015

Thank you for sharing!

@nareshsplashmath

This comment has been minimized.

Show comment
Hide comment
@nareshsplashmath

nareshsplashmath May 29, 2017

My application is on rails 3.2 from start and I am getting the same error but I have never done any upgrading from rails 3 to rails 4.

My application is on rails 3.2 from start and I am getting the same error but I have never done any upgrading from rails 3 to rails 4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment