Ruby On Rails Lesson
Now that we’ve finished a site layout with decent styling, it’s time to start filling in the links we’ve stubbed out with ’#’. Of course, we could hard-code links like
but that isn’t the Rails Way. For one, it would be nice if the URI for the About page were /about rather than /static pages/about; moreover, Rails conventionally uses named
routes, which involves code like
This way the code has a more transparent meaning, and it’s also more flexible since we can change the definition of about_path and have the URI change everywhere about_path is used.
The full list of our planned links appears in Table 5.1, along with their mapping to URIs and routes. We’ll implement all but the last one by the end of this chapter. (We’ll make the last one in Chapter 8.)
Before moving on, let’s add a Contact page (left as an exercise in Chapter 3). The test appears as in Listing 5.16, which simply follows the model last seen in Listing 3.18. Note that, as in the application code, in Listing 5.16 we’ve switched to Ruby 1.9– style hashes.
Table 5.1 Route and URI mapping for site links.
Listing 5.16 Tests for a Contact page.
You should verify that these tests fail: