Routes

The routing module provides URL rewriting in native Ruby. It’s a way to redirect incoming requests to controllers and actions. It replaces the mod_rewrite rules. Best of all, Rails’ Routing works with any web server. Routes are defined in app/config/routes.rb.

A route is the part of the URL that determines how an incoming HTTP request is directed to the appropriate controller and action for processing. It tells Rails which controller and action should respond to a request.

Think of creating routes as drawing a map for your requests. The map tells them where to go based on some predefined pattern −

Rails.application.routes.draw doPattern1 tells some request to go to one place
   Pattern2 tell them to go to another
   ...end

When Rails sees a request that matches any of the patterns, it will send the request to the corresponding controller and the appropriate action inside of that controller.

There are 4 common actions you will generally need for a resource: CreateReadUpdateDelete (CRUD). This translates to 7 typical routes:

ActionHTTP VerbPathPurpose
indexGET/booksList all records
showGET/books/:idShow a single record
newGET/books/newDisplay a form for a new record
createPOST/booksSave a new record to the database
editGET/books/:id/editDisplay a form to edit a record
updatePATCH/PUT/books/:idUpdate an existing record
destroyDELETE/books/:idDelete a record

Typing out these routes every time is redundant, so Rails provides a shortcut for defining them. To create all of the same CRUD routes, replace the above routes with this single line.

Example

Let us consider our library management application contains a controller called BookController. We have to define the routes for those actions which are defined as methods in the BookController class.

Open routes.rb file in library/config/ directory and edit it with the following content.

Rails.application.routes.draw do
  resources :booksend

If you decide to define only a certain routes instead of all the default routes, you can put the required routes in a list.

Rails.application.routes.draw do
  resources :books, only:%i[index show create]end

You may also define the routes explicitly by mentioning the respective HTTP verbs

Rails.application.routes.draw do
  get 'books/'
  get 'books/show'
  post 'books/create'end

This works because Rails automatically maps get ‘books/index’ to BooksController#index.

If the path does not directly match the controller#action, you must specify to: explicitly:

get 'all-books', to:'books#index'# Custom URL: /all-books
get 'book-view/:id', to:'books#show'# Custom URL: /book-view/:id

If your application has multiple models (e.g., Book and Employee), you should have separate resources statements for each model in config/routes.rb.

Rails.application.routes.draw do
  resources :books
  resources :employeesend

The routes.rb file defines the actions available in the applications and the type of action such as get, post, and patch.

Use the following command to list all your defined routes, which are useful for tracking down routing problems in your application, or giving you a good overview of the URLs in an application you’re trying to get familiar with.

rails routes

In the older version of Ruby on Rails (before version 5), you would use:

rake routes

What is Next?

Next, we will create the code to generate screens to display data and to take input from the user.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *