Skip to content

Instantly share code, notes, and snippets.

@mjtko
Created October 12, 2011 15:48
Show Gist options
  • Save mjtko/1281582 to your computer and use it in GitHub Desktop.
Save mjtko/1281582 to your computer and use it in GitHub Desktop.
middleware that preserves flashes over multiple redirects
# use via this in application.rb:
# config.middleware.insert_after(ActionDispatch::Flash, FlashPreserver)
class FlashPreserver
def initialize(app)
@app = app
end
def call(env)
flash = (session = env['rack.session']) && session['flash']
@app.call(env).tap do |status,*rest|
flash.keep if flash && [301, 302, 307].include?(status)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment