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.

Brand-new engine<br />

HTTP status code, then Rack moves on to the next one, going all the way through<br />

each part of the stack, moving on to the next every time the current part returns a 404<br />

until it hits the application. The application is the final stop of this request, and so<br />

whatever it returns goes. In this case, the application returns a 200 (“OK”) request,<br />

which is then passed back to the client through Rack.<br />

If a non-404 response was ever returned from a middleware object, then the digging<br />

would stop there, and the response would climb back to the surface.<br />

In a Rails application, the routing is actually a piece of the middleware stack. As<br />

the request goes through the chain, it eventually comes to the routes, which then<br />

determine where the request should head. When routing requests to an application,<br />

the sequence looks like figure 17.3.<br />

In this example, Rack still receives the request and goes through the middleware<br />

stack, with each middleware passing the buck until it comes up to the routes. This then<br />

handles the request, routing the request to (usually) a controller, which then executes<br />

the necessary code to get the job done and passes the result back up to the client.<br />

An engine is served in much the same way; you mount it at a specific path in your<br />

application’s config/routes.rb line with this line:<br />

mount Forem::Engine, :at => "/forem"<br />

Any request to the /forem path will not be passed to the application, but instead<br />

passed to the engine. The engine then decides how to route that request and returns<br />

a response exactly like an application. This process is shown in figure 17.4.<br />

2. Goes through middleware<br />

stack until...<br />

1. Request comes in for<br />

GET /projects<br />

Middleware #1<br />

Middleware #n<br />

3. Routes receive request<br />

Browser<br />

Server<br />

... Application Engine<br />

Routes<br />

7. Response goes out<br />

6. Application responds<br />

5. That route goes to<br />

the application!<br />

477<br />

4. What route is it?<br />

GET /projects Figure 17.3 Application route cycle

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

Saved successfully!

Ooh no, something went wrong!