Plugging Rack into Rails
The interwebs are all a’buzz about Rack. For those that haven’t been following along, Rack is a specification and a library for connecting web servers to Ruby libraries (a.k.a. “Middleware”). It’s basically the Web 2.0 version of CGI, except that it doesn’t suck and it’s just for Ruby.
Rails 2.3 has Rack baked in. It uses Rack for things like sessions and parameter parsing. But what if you want to add your own middleware to a Rails app?
It’s really easy! In the init.rb
of a plugin, or just in a Rails initializer:
This will append your middleware to the end of the “stack”, so it will be executed after all of Rails’ middleware, but before anything else in the Rails framework.
But what if you need to massage request parameters before Rails parses them? All you need to do is insert your middleware in the stack before Rails parses the params. You can find the current list of middleware by inspecting ActionController::Dispatcher.middleware
. In there you’ll find ActionController::ParamsParser
, which is what we want to insert our middleware before. Unfortunately, there’s not an insert_before
method (yet), so we’ll need to use insert_after
, giving it a middleware earlier in the stack:
If you don’t like the way that one of the existing pieces of middleware handles things, you can swap it out for your own version:
So there you have it. Eat, drink, and go write middleware.