Routing in Rails

What exactly is routing?

When there’s an HTTP request from the user to the application, it should be directed to the right controller. You can picture a router as a receptionist at who connects you to the right person to talk to.

How is routing d…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Mazin Idris

Image description

What exactly is routing?

When there's an HTTP request from the user to the application, it should be directed to the right controller. You can picture a router as a receptionist at who connects you to the right person to talk to.

Image description

How is routing done in Ruby on Rails?
In Rails, the routes of your application live in config/routes.rb. The Rails router recognises URLs and dispatches them to a controller's action. It can also generate paths and URLs, avoiding the need to hardcode strings in your views. Let's consider an application to book rooms in different Hotels and take a look at how this works.

Types of HTTP request methods:

The application receives an HTTP request which carries along with it a method which could be:

  • GET - Retrieve a resource
  • POST - Create a resource
  • PUT - Completely update a resource
  • PATCH - Partially update a resource
  • DELETE - Delete a resource

These methods determine which controller action method is called.

Image description

Decoding the http request

If the application receives the request, GET /players/1. The request is dispatched to the players controller's show action with { id: '1' } in params.

get '/teams/:id', to: 'teams#show'

Similarly,

get '/teams', to: 'teams#index'
get '/teams/:id', to: 'teams#show'
get '/teams/new', to: 'teams#new'
post '/teams', to: 'teams#create'
get '/teams/:id/edit', to: 'teams#edit'
put '/teams/:id', to: 'teams#update'
delete '/teams/:id', to: 'teams#destroy'

Defining resources

Like how the receptionist holds a record of all the reservations, the application too has its own record in config/routes.rb.

Rails.application.routes.draw do
  resources :teams
end

By writing resources :teams we create all seven different routes in your application, all mapping to the Hotels controller like mentioned above.

If your controller has only a few of these actions you can alter the same with the following keywords.

The keyword only includes only the actions mentioned. resources :teams, only: [:edit]

There's also the keyword except to name the ones you don't want to include.

resources :teams, except: [:index]

resources :players,
resources :teams

Nested Resources
Sometimes we have nested routes, /teams/:id/players which are a result of resources that are logically children of other resources. For example, suppose your application includes these models:

class Team < ApplicationRecord
  has_many :players
end

class Player < ApplicationRecord
  belongs_to :teams
end

Image description

In which case we will declare our resources this way,'

resources :teams do
  resources :players
end

This declaration helps us access the nested URLs such as /teams/:id/players , /teams/:id/players/:id , /teams/:id/players/new etc


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Mazin Idris


Print Share Comment Cite Upload Translate
APA
Mazin Idris | Sciencx (2023-03-31T02:51:09+00:00) » Routing in Rails. Retrieved from https://www.scien.cx/2022/12/12/routing-in-rails/.
MLA
" » Routing in Rails." Mazin Idris | Sciencx - Monday December 12, 2022, https://www.scien.cx/2022/12/12/routing-in-rails/
HARVARD
Mazin Idris | Sciencx Monday December 12, 2022 » Routing in Rails., viewed 2023-03-31T02:51:09+00:00,<https://www.scien.cx/2022/12/12/routing-in-rails/>
VANCOUVER
Mazin Idris | Sciencx - » Routing in Rails. [Internet]. [Accessed 2023-03-31T02:51:09+00:00]. Available from: https://www.scien.cx/2022/12/12/routing-in-rails/
CHICAGO
" » Routing in Rails." Mazin Idris | Sciencx - Accessed 2023-03-31T02:51:09+00:00. https://www.scien.cx/2022/12/12/routing-in-rails/
IEEE
" » Routing in Rails." Mazin Idris | Sciencx [Online]. Available: https://www.scien.cx/2022/12/12/routing-in-rails/. [Accessed: 2023-03-31T02:51:09+00:00]
rf:citation
» Routing in Rails | Mazin Idris | Sciencx | https://www.scien.cx/2022/12/12/routing-in-rails/ | 2023-03-31T02:51:09+00:00
https://github.com/addpipe/simple-recorderjs-demo