Learn Ruby on Rails Book

Basics of Ruby on Rails routing

What is routing

Routing is the term used to denote the process of mapping URLs, or specifically request paths, to controller actions. In other words, it is used to define which method should be executed when a request is received to a specific path.

We will be defining all our request endpoints and their corresponding controller actions in config/routes.rb.

How Rails routing works

Assume we have defined a route like this:

1get "/articles" => "articles#list"

But wait! What is the => symbol?

The => symbol is known as the hashrocket. It's used to denote Hash literals in Ruby. But with newer versions of Ruby and Rails, we prefer to use JSON styled Hash notation, where we use a colon.

But still the => can be found in many places, predominantly in the routes.rb and Gemfile.

Coming back to routing, here is how it will work:

  • When a user visits http://localhost:3000/articles Rails will look up all our defined routes for patterns matching /articles.
  • In this case, we have only one entry and its value corresponds to articles#list.
  • Rails will split that string into two parts: articles and list.
  • The first part, articles, will be used to search for a controller with a matching name. In this case, Rails will be searching for the class ArticlesController.
  • The second part, list, will be considered as the name of the method to be called.
  • Now, Rails will run the method list defined inside ArticlesController class and the result will be returned to the client.

Let's take another example:

1get "/books" => "books#list"

Here if the request is made to /books then Rails will send the request to BooksController and action would be list.

Dynamic segments in routes

Let's say that we want to see the details of the book The Fountainhead.

The URL could look like http://localhost:3000/books/the-fountainhead. What should the route configuration be for this?

We can have something like this:

1get "/books/the-fountainhead" => "books#the_fountainhead"

If we have thousands of books, or if the books are dynamically added, this strategy will not scale. We can't manually keep making an entry in the routes file for each book.

Dynamic Segments are there to rescue us in such situations. This is how we define routes using Dynamic segment approach.

1get "/books/:id" => "books#show"

In the above case when user visits /books/the-fountainhead, Rails will send the request to show action inside BooksController class.

Also, Rails will make the request path segment corresponding to :id available in params method. The value of params behaves like a hash.

We can access the value of :id from show action like this:

1class BooksController < ApplicationController
2  def show
3    book_name = params[:id]
4    # get the book details from the database
5  end
6end

Root URL

Root URL determines what to respond when a user tries to load the home page of our application. In our case, it will be http://localhost:3000.

We will define route to the root URL like this:

1root to: "home#welcome"

In the above case when a user visits http://localhost:3000, the welcome action in the HomeController will be triggered.

More about Rails routing in the official guide

There is a lot more to explore in Rails routing. The official Rails guide on Rails routing has some excellent material on it.

Here are some must reads.

We haven't made any changes in our project. So there is nothing to commit in this chapter.

⌘K
    to navigateEnterto select Escto close
    Previous
    Next