27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

534 CHAPTER 18 Rack-based applications<br />

You can run the rake middleware command within your Rails<br />

application’s directory to see the list of middleware currently in<br />

use by your Rails application:<br />

use ActionDispatch::Static<br />

use Rack::Lock<br />

use ActiveSupport::Cache::Strategy::LocalCache<br />

use Rack::Runtime<br />

...<br />

use ActionDispatch::BestStandardsSupport<br />

use Warden::Manager<br />

run Ticketee::Application.routes<br />

Each of these middleware pieces performs its own individual function.<br />

For instance, the first middleware ActionDispatch::Static<br />

intercepts requests for static files such as images, JavaScript files,<br />

or stylesheets found in public and serves them immediately, without<br />

the request to them falling through to the rest of the stack. It’s<br />

important to note that this middleware is only active in the development<br />

environment, as in production your web server (such as<br />

nginx) is better suited for serving static assets.<br />

Other middleware, such as ActionDispatch::BestStandards-<br />

Support, sets additional headers on your request. This particular piece of middleware<br />

sets the X-UA-Compatible header to IE=Edge,chrome=1, which tells Microsoft Internet<br />

Explorer to “display content in the highest mode available” that is “equivalent to IE9<br />

mode,” meaning your pages should render in a “best standards” fashion. 6 The<br />

chrome=1 part of this header is for the Google Chrome Frame, which again will support<br />

“best standards” rendering on a page.<br />

Let’s look at both of these middleware pieces now.<br />

18.4.1 Middleware in Rails<br />

Browser<br />

Web Server<br />

In the case of the ActionDispatch::Static middleware, a response is returned when<br />

it finds a file to serve, and the request stops there. In the case of Action-<br />

Dispatch::BestStandardsSupport, the request is modified and allowed to continued<br />

down the chain of middleware until it hits Ticketee::Application.routes, which<br />

will serve the request using the routes and code in your application. The process of<br />

ActionDispatch::Static can be seen in figure 18.4.<br />

When a request is made to /images/rails.png, the middleware checks to see if the<br />

public/images/rails.png file exists. If it does, then it is returned as the response of<br />

this request. This middleware will also check for cached pages. If you make a request<br />

to /projects, Rails (by default) will first check to see if a public/projects.html file<br />

exists before sending the request to the rest of the stack. This type of request is<br />

shown in figure 18.5.<br />

6 For more information about IE=Edge and the X-UA-Compatible header, see http://msdn.microsoft.com/<br />

en-us/library/cc288325(v=vs.85).aspx.<br />

Rack<br />

Application Stack<br />

Middleware<br />

...<br />

Middleware<br />

Application<br />

Figure 18.3 Full<br />

request stack, redux

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!