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.1 Test-driven development

In test-driven development, we first write a failing test, represented
in many testing tools by the color red. We then implement code to get
the test to pass, represented by the color green. Finally, if necessary,
we refactor the code, changing its form (by eliminating duplication, for
example) without changing its function. This cycle is known as “Red,
Green, Refactor
3.3.2 Passing title tests

Now we’ll get our title tests to pass, and at the same time add the full
HTML structure needed to make valid web pages. The markup for a modern
web page follows this form:

<!DOCTYPE html>
<html>
  <head>
    <title>Greeting</title>
  </head>
  <body>
    <p>Hello, world!</p>
  </body>
</html>

This structure includes a document type, or doctype, declaration at the
top to tell browsers which version of HTML we’re using (in this case,
HTML5);8 a head section, in this case with “Greeting” inside a title
tag; and a body section, in this case with “Hello, world!” inside a p
(paragraph) tag. (The indentation is optional—HTML is not sensitive to
whitespace, and ignores both tabs and spaces—but it makes the document’s
structure easier to see.)

Applying this basic structure to the Home page yields Listing 3.20.