About Rails Routing

Tram Ho

Introduce

Resource routing allows us to quickly declare common routes for the controller. Instead of declaring each route separately: index, show, new, edit, create, update, destroy:

Resource route will help us to declare them only 1 line:

Resources on the browser

The browser requests pages from Rails by making requests for the URL using a specific HTTP method, such as GET, POST, PATCH, PUT, and DELETE.

Each method is a request to perform an operation on a resource. Each resource route maps to a number of but requests related to actions in a certain controller

For example, when we receive a request of the form DELETE /posts/33

=> It will call the router to map to the correct destroy action in PostsController and with the parameter passed to the function id of post is 33 with DELETE

Resources and Resources

Rails supports creating routes for a full restful routes object with just a single command line. We can use resources or resources to declare but there is a little difference between these two declarations.

When using resources, Rails generates 7 different routes in the application.

Example resources :photos

Especially in Rails also supports us to create Singular Resources. This type of resources will be used in case we want to manipulate a single record in an example table when we want to display someone’s account update page without knowing this person’s id

When using resources, because it only needs to work with one object, Rails only generates 6 new routes and subtracts one route which is the index.

Example: resource :geocoder

Nested resources

Nested Resources is a technique in Rails that is used to reflect the has_many relationship between models in routes and then render it via URLs. Using Nested Resources makes the code easier to understand, helping DRY (don’t repeat yourself) code.

For example, we have 2 models with has_many relationship:

=> A Magazine will have many Ads and 1 Ad belongs to 1 Magazine

In the config / routes.rb file, configure the following:

We will have the following ads routes:

HTTP VerbPathController # ActionUsed for
GET/ magazines /: magazine_id / adsads # indexdisplay a list of all ads for a specific magazine
GET/ magazines /: magazine_id / ads / newads # newreturn an HTML form for creating a new ad belonging to a specific magazine
POST/ magazines /: magazine_id / adsads # createcreate a new ad belonging to a specific magazine
GET/ magazines /: magazine_id / ads /: idads # showdisplay a specific ad belonging to a specific magazine
GET/ magazines /: magazine_id / ads /: id / editads # editreturn an HTML form for editing an ad belonging to a specific magazine
PATCH / PUT/ magazines /: magazine_id / ads /: idads # updateupdate a specific ad belonging to a specific magazine
DELETE/ magazines /: magazine_id / ads /: idads # destroydelete a specific ad belonging to a specific magazine

Also create urls / paths for example magazine_ads_url and edit_magazine_ad_path . These helpers will require an input parameter that is an instance of magazine: magazine_ads_url @magazine

Controller Namespaces and Routing

Rails allows you to group controllers into namespaces using the namespace keyword. Example grouping Articles and Comments controller in Admin controller.

Meanwhile, to view the articles you must add the admin prefix to the url: /admin/articles

There will still be 7 routes generated as follows:

HTTP VerbPathController # ActionNamed Helper
GET/ admin / articles/ admin / articles # indexadmin_articles_path
GET/ admin / articles / new/ admin / articles # newnew_admin_articles_path
POST/ admin / articles/ admin / articles # createadmin_articles_path
GET/ admin / articles /: id/ admin / articles # showadmin_articles_path (: id)
GET/ admin / articles /: id / edit/ admin / articles # editedit_admin_articles_path (: id)
PATCH / PUT/ admin / articles /: id/ admin / articles # updateadmin_articles_path (: id)
DELETE/ admin / articles /: id/ admin / articles # destroyadmin_articles_path (: id)

Member and Collection

If we want to add routes outside of RESTful routes, Rails supports creating additional routes using members or collections.

Member routes

We will place the member block in the resources block

=> The generated path will be in the form / photos / 1 / preview with the method GET and it will call the preview function in PhotosController with params [: id] as the parameter passed. It also generates 2 preview_photo_url and preview_photo_path helper

Collection Routes

To add a collection route

=> Collection routes in / photos / search form with method GET and it will call the search function in PhotosController.

Collection routes are used in case the user does not want the path to contain the id of the resources it contains

Reference source

https://guides.rubyonrails.org/routing.html#resource-routing-the-rails-default

Share the news now

Source : Viblo