6.1. Introduction

In Section 3.3, “Creating HTML Dynamically with erb” and Chapter 5, Scaffolding and REST we have already come across routes. The routes contained in config/routes.rb define what happens in the Rails application when a user of a Rails application opens a URL. A route can be static and dynamic and pass any dynamic values with variables to the controller. If several routes apply to a URL, the one that is listed at the top of config/routes.rb wins.


If you do not have much time, you can skip this chapter for now and get back to it later if you have any specific questions.
Let's first build a test Rails application so we can experiment:
$ rails new shop
$ cd shop
With rake routes we can display the routes of a project. Let's try it straight away in the freshly created project:
$ rake routes

Nothing. It's a new Rails project, there are no routes yet.


Even in a new Rails project you can access after starting the Rails server with rails server. How can this work without a defined route? The solution lies in the directory /public. This contains the file public/index.html, and whenever the directory /public contains a file that matches the specified URL, this gets a higher priority than a Rails route. So if no route is defined, the program automatically looks in public for index.html.
In case of a new Rails project, the file config/routes.rb has many commented out examples. For the rest of this chapter we pretend that these examples are not there and only display the newly entered routes.

Updates about this book will be published on my Twitter feed.