Rack::Tidy and Devise in the Rack Middleware Stack
25 Sep 2010
After quite a bit of digging around, and a little help from mr. Jose Valim at Plataformatec, I realized that the combination of the Devise authentication gem, along with Rack-Tidy, aren’t quite so friendly with each other. The main culprit here, I would say is the Tidy gem. Why? Because the essence of its existence is to re-arrange the markup handed back from the app-server. So some things get lost in the shuffle during all that house-cleaning (please, correct me if I’m wrong).
I had a hunch that with a little musical chairs in the middleware stack, we could find a solution that would allow all pieces to live harmoniously. Luckily, I was right. The trick is to make sure Tidy is inserted into the stack before ActionDispatch::Flash (because Rack::Tidy was killing the flash messages returned from Devise/Warden) and before Warden::Manager (the rack authentication layer beneath Devise). The resulting stack, for me, looks like so (important bits in bold):
And is accomplished with this code instead the app initialization process (application.rb):
config.middleware.delete ActionDispatch::Flash # remove from current position
config.middleware.insert_before Warden::Manager, ActionDispatch::Flash # add it right back in before Warden
config.middleware.insert_before ActionDispatch::Flash, Rack::Tidy, 'indent-spaces' => 2 # finally, add in Rack:Tidy before ActionDispatch::Flash.
The resulting stack looks like it’s working quite well for now.