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:
1 2 3 | get "users/index" post “users/update” |
Resource route will help us to declare them only 1 line:
1 2 | resources users |
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:
1 2 3 4 | class Magazine < ApplicationRecord has_many :ads end |
1 2 3 4 | class Ad < ApplicationRecord belongs_to :magazine end |
=> A Magazine will have many Ads and 1 Ad belongs to 1 Magazine
In the config / routes.rb file, configure the following:
1 2 3 4 | resources :magazines do resources :ads end |
We will have the following ads routes:
HTTP Verb | Path | Controller # Action | Used for |
---|---|---|---|
GET | / magazines /: magazine_id / ads | ads # index | display a list of all ads for a specific magazine |
GET | / magazines /: magazine_id / ads / new | ads # new | return an HTML form for creating a new ad belonging to a specific magazine |
POST | / magazines /: magazine_id / ads | ads # create | create a new ad belonging to a specific magazine |
GET | / magazines /: magazine_id / ads /: id | ads # show | display a specific ad belonging to a specific magazine |
GET | / magazines /: magazine_id / ads /: id / edit | ads # edit | return an HTML form for editing an ad belonging to a specific magazine |
PATCH / PUT | / magazines /: magazine_id / ads /: id | ads # update | update a specific ad belonging to a specific magazine |
DELETE | / magazines /: magazine_id / ads /: id | ads # destroy | delete 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.
1 2 3 4 | namespace :admin do resources :articles end |
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 Verb | Path | Controller # Action | Named Helper |
---|---|---|---|
GET | / admin / articles | / admin / articles # index | admin_articles_path |
GET | / admin / articles / new | / admin / articles # new | new_admin_articles_path |
POST | / admin / articles | / admin / articles # create | admin_articles_path |
GET | / admin / articles /: id | / admin / articles # show | admin_articles_path (: id) |
GET | / admin / articles /: id / edit | / admin / articles # edit | edit_admin_articles_path (: id) |
PATCH / PUT | / admin / articles /: id | / admin / articles # update | admin_articles_path (: id) |
DELETE | / admin / articles /: id | / admin / articles # destroy | admin_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
1 2 3 4 5 6 | resources :photos do member do get 'preview' end end |
=> 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
1 2 3 4 5 6 | resources :photos do collection do get 'search' end end |
=> 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