Listing 3.4: Generating a StaticPages controller.

Rails comes with a script for making controllers called generate; all it
needs to work its magic is the controller’s name. Since we’ll be making
a controller to handle static pages, we’ll call it the StaticPages
controller. We’ll also plan to make actions for a Home page, a Help
page, and an About page. The generate script takes an optional list of
actions, so we’ll include two of the initial actions directly on the
command line (Listing 3.4).

Here we’ve used the option --no-test-framework to suppress the
generation of the default RSpec tests, which we won’t be using. Instead,
we’ll create the tests by hand starting in Section 3.2. We’ve also
intentionally left off the about action from the command line arguments
in Listing 3.4 so that we can see how to add it using test-driven
development, or TDD (Section 3.2).
3.2.2 Adding a page

Having seen test-driven development in action in a simple example, we’ll
use the same technique to accomplish the slightly more complicated task
of adding a new page, namely, the About page that we intentionally left
off in Section 3.1. By writing a test and running RSpec at each step,
we’ll see how TDD can guide us through the development of our
application code.
3.3.4 Eliminating duplication with layouts

Now that we’ve replaced the variable part of the page titles with ERb,
each of our pages looks something like this:

<% provide(:title, 'Foo') %>
<!DOCTYPE html>
<html>
  <head>
    <title>Ruby on Rails Tutorial Sample App | <%= yield(:title)
%></title>
  </head>
  <body>
      Contents
  </body>
</html>

In other words, all our pages are identical in structure, including the
contents of the title tag, with the sole exception of the material
inside the body tag.

In order to factor out this common structure, Rails comes with a special
layout file called application.html.erb, which we renamed in Section
3.3.1 and which we’ll now restore:

$ mv foobar app/views/layouts/application.html.erb

To get the layout to work, we have to replace the default title with the
Embedded Ruby from the examples above:

<title>Ruby on Rails Tutorial Sample App | <%= yield(:title) %></title>