<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>opensoul.org - Plugging Rack into Rails Changes</title>
  <id>tag:www.opensoul.org,2009:/2009/3/3/plugging-rack-into-rails/changes</id>
  <generator uri="http://mephistoblog.com" version="0.8.0">Mephisto Drax</generator>
  <link href="http://www.opensoul.org/2009/3/3/plugging-rack-into-rails/changes.xml" rel="self" type="application/atom+xml"/>
  <link href="/2009/3/3/plugging-rack-into-rails" rel="alternate" type="text/html"/>
  <updated>2009-03-03T13:30:27Z</updated>
  <entry xml:base="http://www.opensoul.org/">
    <author>
      <name>brandon</name>
    </author>
    <id>tag:www.opensoul.org,2009-03-03:522:1</id>
    <updated>2009-03-03T13:30:27Z</updated>
    <link href="http://www.opensoul.org/2009/3/4/plugging-rack-into-rails" rel="alternate" type="text/html"/>
    <title>Plugging Rack into Rails</title>
<content type="html">&lt;p&gt;The interwebs are all a&#8217;buzz about &lt;a href=&quot;http://rack.rubyforge.org/&quot;&gt;Rack&lt;/a&gt;. For those that haven&#8217;t been following along, Rack is a &lt;em&gt;specification&lt;/em&gt; and a &lt;em&gt;library&lt;/em&gt; for connecting web servers to Ruby libraries (a.k.a. &#8220;Middleware&#8221;). It&#8217;s basically the Web 2.0 version of &lt;span class=&quot;caps&quot;&gt;CGI&lt;/span&gt;, except that it doesn&#8217;t suck and it&#8217;s just for Ruby.&lt;/p&gt;


	&lt;p&gt;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?&lt;/p&gt;


	&lt;p&gt;It&#8217;s really easy! In the &lt;code&gt;init.rb&lt;/code&gt; of a plugin, or just in a Rails initializer:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;ActionController::Dispatcher.middleware.use MyMiddleware&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;This will append your middleware to the end of the &#8220;stack&#8221;, so it will be executed after all of Rails&#8217; middleware, but before anything else in the Rails framework.&lt;/p&gt;


	&lt;p&gt;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. Here is the &lt;em&gt;current&lt;/em&gt; list of middleware used by Rails:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;&lt;code&gt;Rack::Lock&lt;/code&gt;&lt;/li&gt;
		&lt;li&gt;&lt;code&gt;ActionController::Failsafe&lt;/code&gt;&lt;/li&gt;
		&lt;li&gt;&lt;code&gt;ActionController::Session::CookieStore&lt;/code&gt;&lt;/li&gt;
		&lt;li&gt;&lt;code&gt;ActionController::Session::MemCacheStore&lt;/code&gt;&lt;/li&gt;
		&lt;li&gt;&lt;code&gt;ActiveRecord::SessionStore&lt;/code&gt;&lt;/li&gt;
		&lt;li&gt;&lt;code&gt;Rails::Rack::Metal&lt;/code&gt;&lt;/li&gt;
		&lt;li&gt;&lt;code&gt;ActionController::RewindableInput&lt;/code&gt;&lt;/li&gt;
		&lt;li&gt;&lt;code&gt;ActionController::ParamsParser&lt;/code&gt;&lt;/li&gt;
		&lt;li&gt;&lt;code&gt;Rack::MethodOverride&lt;/code&gt;&lt;/li&gt;
		&lt;li&gt;&lt;code&gt;Rack::Head&lt;/code&gt;&lt;/li&gt;
		&lt;li&gt;&lt;code&gt;ActiveRecord::QueryCache&lt;/code&gt;&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;So you would need to insert it before &lt;code&gt;ActionController::ParamsParser&lt;/code&gt;:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;ActionController::Dispatcher.middleware.insert_after 'ActionController::Failsafe', MyMiddleware&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Unfortunately, there&#8217;s not an &lt;code&gt;insert_before&lt;/code&gt; method (yet), so I just inserted after the Failsafe.&lt;/p&gt;


	&lt;p&gt;If you don&#8217;t like the way that one of the existing pieces of middleware handles things, you can swap it out for your own version:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;ActionController::Dispatcher.middleware.swap 'Rails::Rack::Metal', HeavyMetal&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;So there you have it. Eat, drink, and go write middleware.&lt;/p&gt;</content>  </entry>
</feed>
