An undeniable theme from RailsConf this year was the arrival of Rack in Rails as of version 2.3. The first topic that seemed to pique developer interest was Rails Metal. Most enthusiasm centered around the speed bump of using a lighter stack for high volume URIs. While this is a useful upgrade to Rails, the architectural shift provided by Rack brings new flexibility. In this post, I will demonstrate the unique value of mounting Rack middleware in your Rails stack.
A Rails app serving API data can now provide a third option without opening security holes or requiring new infrastructure for client sites.
AJACSS Sounds Like AJAX
Shopping for Rack Middleware
If you're in need of a piece of Rack middleware, there are two places to check before rolling your own -- the Rack wiki and rack-contrib on GitHub. In the case of CSSHttpRequest, we can just grab a pre-made piece of middleware and drop it into our Rails app thanks to Cameron Walters from nb.io.
In config/environment.rb, add the following code inside the Rails::Initializer block:
That's it! Rack::CSSHTTPRequest will automatically encode the output for any request using the "_format=chr" convention used by Rails. Other web apps and services can use the data without exposing security holes or requiring new infrastructure.