I will keep a memorandum about the resources method.
-The resources
method is a method to write in the ** routes.rb ** file.
-You can add the routing of 7 actions that are the basis of Rails at once.
The seven basic actions are listed below.
action | HTTP | role | URL |
---|---|---|---|
index | get | Display a list of resources. | /users |
show | get | View resource details. | /users/:id |
new | get | Create a new resource. | /users/new |
create | post | Create a new resource and save it. | /users |
edit | get | Edit the resource. | /users/:id/edit |
update | put/patch | Update resources. | /users/:id |
destroy | delete | Delete the resource. | /users/:id |
First, check the routing without defining the resources method, and make sure nothing is defined.
Next, define the resources
method as follows.
routes.rb
Rails.application.routes.draw do
#Resources method define
resources :users
end
If you check the routing after defining it ... You can see that the routing of 7 basic actions has been added.
Prefix Verb URI Pattern Controller#Action
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
From now on, let's take a look at the options for the resources
method.
only It can be used when you want to specify only a specific action out of the 7 actions.
routes.rb
Rails.application.routes.draw do
#create action and new action only
resources :users, only:[:create, :new]
end
Looking at the routing, it looks like this.
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
memeber
The member
method can add actions other than the seven actions.
First, the definition method is described below.
routes.rb
Rails.application.routes.draw do
#How to define member method
resources :users do
member do
get :following, :followers
end
end
By defining in this way, the following routing can be obtained in addition to the seven actions.
The member
method allows you to define actions for individual resources ** specified by ** id.
Prefix Verb URI Pattern Controller#Action
following_user GET /users/:id/following(.:format) users#following
followers_user GET /users/:id/followers(.:format) users#followers
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
collection
Like the member
method, the collection
method can add actions other than the seven actions.
The difference from the member
method is that the collection
method defines ** actions ** for the entire resource.
The definition method is the same as the member
method.
routes.rb
Rails.application.routes.draw do
#How to define collection method
resources :users do
collection do
get :following, :followers
end
end
If you look at the routing, you can see that it is added in addition to the 7 actions like the member
method.
However, the part of URI Pattern
is different.
In the collection
method, the action is defined for all resources, so the **:/id ** part is omitted.
Prefix Verb URI Pattern Controller#Action
following_user GET /users/following(.:format) users#following
followers_user GET /users/followers(.:format) users#followers
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
** [Comparison of two methods] **
Prefix Verb URI Pattern Controller#Action
##member method
following_user GET /users/:id/following(.:format) users#following
followers_user GET /users/:id/followers(.:format) users#followers
##collection method
following_user GET /users/following(.:format) users#following
followers_user GET /users/followers(.:format) users#followers
Rails tutorial Chapter 7 User registration https://railstutorial.jp/chapters/sign_up?version=4.2
Rails tutorial Chapter 14 Follow users https://railstutorial.jp/chapters/following_users?version=6.0#cha-following_users
[Rails] Let's define the routing using the resources method! https://pikawaka.com/rails/resources
Recommended Posts