27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

476 CHAPTER 17 Engines<br />

This file’s second line of real code here requires the test/dummy/config/environment<br />

.rb file, which loads the application in test/dummy.<br />

TEST/DUMMY<br />

The application contained within this directory is purely for testing purposes, but you<br />

can set it up to act like a real application by creating controllers, helpers, models,<br />

views, and routes if you wish. When the tests run, this application is initialized like a<br />

real Rails application.<br />

Your tests then run against this application, which has your engine mounted inside<br />

the test/dummy/config/routes.rb file with this line:<br />

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

This line mounts the routes of your engine (not yet defined) at the /forem path of<br />

your application. This means that whenever you want to access this engine, you must<br />

prefix the route with /forem. If you are in code, you can use the forem. prefix for the<br />

routing helpers, as seen earlier with forem.forums_path or forem.root_path within<br />

the dummy application.<br />

Even though there are a lot of files that are generated for engines—like there are<br />

when you generate an application—they all play a crucial role in your engine. Without<br />

this lovely scaffold, you’d have to create it all yourself, which would be no fun.<br />

This routing may be a little confusing at first, but it’s quite simple. Let’s look into<br />

how this process works, and then you’ll get into developing your first feature for this<br />

engine.<br />

17.3.3 Engine routing<br />

In an application, you need to define routes to an<br />

engine when you’re using it. You can do that with this<br />

line in config/routes.rb:<br />

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

To understand how engines are routed, you must understand<br />

the concept of middleware within a Rails application.<br />

14 Middleware is the term used for code that sits<br />

between the receiving server and the application; middleware<br />

can do a number of things, such as serve static<br />

assets and set flash messages. Rails 3 applications run on<br />

Rack, which uses a stack-based architecture to accomplish<br />

a request. A basic stack is shown in figure 17.2.<br />

In this picture, a basic request cycle for a Rails application<br />

is shown. A request comes from a client and hits<br />

Rack first, which then goes through all of the middleware.<br />

If a middleware returns a 404 (“Not Found”)<br />

14 Additional information about middleware can be found in chapter 18.<br />

Middleware #1<br />

404<br />

Middleware #1<br />

404<br />

Client<br />

Rack<br />

Application<br />

Figure 17.2<br />

A simple middleware stack<br />

200

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

Saved successfully!

Ooh no, something went wrong!