[Ruby on Rails] Use the resources method to automatically create routes.

Introduction

I will keep a memorandum about the resources method.

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.

7 basic Rails actions

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

How to define

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

resources method options

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

References

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

[Ruby on Rails] Use the resources method to automatically create routes.
[Ruby on Rails] How to use session method
How to use Ruby on Rails
[Ruby on Rails] How to use CarrierWave
[Rails] How to use the map method
[Ruby on Rails] How to use redirect_to
(Ruby on Rails6) Create a function to edit the posted content
[Ruby basics] How to use the slice method
[Introduction] Try to create a Ruby on Rails application
Method summary to update multiple columns [Ruby on Rails]
(Ruby on Rails6) How to create models and tables
[Ruby On Rails] How to use simple_format to display the entered text with line breaks
Ruby on Rails record search, create if not find_or_create_by method
[Ruby on Rails] Automatically enter the address from the zip code
How to use the link_to method
How to use the include? method
How to use the form_with method
[Ruby] How to use any? Method
[Rails 6] destroy using the resources method
[Ruby on Rails] Convenient helper method
How to use Ruby inject method
[Rails] Don't use the select method just to narrow down the columns!
[Ruby on Rails] "|| =" ← Summary of how to use this assignment operator
Things to remember and concepts in the Ruby on Rails tutorial
[Ruby on Rails] How to avoid creating unnecessary routes for devise
[Ruby on Rails] Pass the parameters divided by date_select to FormObject.
Ruby on Rails installation method [Mac edition]
[Ruby] From the basics to the inject method
part of the syntax of ruby ​​on rails
[Java] How to use the toString () method
Deploy to Heroku [Ruby on Rails] Beginner
Ruby on Rails controller create / delete command
Preparing to introduce jQuery to Ruby on Rails
Beginners create portfolio in Ruby on Rails
[Ruby on Rails] Button to return to top
[Rails] How to use helper method, confimartion
How to create a query using variables in GraphQL [Using Ruby on Rails]
[Error] Switch environment construction to use Ruby on Rails oss (open source)
How to solve the local environment construction of Ruby on Rails (MAC)!
How to debug the processing in the Ruby on Rails model only on the console
[Ruby On Rails] How to search the contents of params using include?
[Rails] How to decide the destination by "rails routes"
Deploy to Ruby on Rails Elastic beanstalk (EB deploy)
[Ruby on Rails] How to display error messages
[Ruby on Rails] Until the introduction of RSpec
How to add / remove Ruby on Rails columns
[Ruby on Rails] NoMethodError undefined method `devise_for'error resolution
[Ruby] How to use gsub method and sub method
Publish the app made with ruby on rails
When you want to use the method outside
Output of how to use the slice method
How to use the replace () method (Java Silver)
Determine the current page with Ruby on Rails
Ruby on Rails address automatic input implementation method
[Ruby on Rails] How to make the link destination part of the specified id
[Ruby on Rails] Change the update date and creation date to your favorite notation
[Ruby on Rails] Rails tutorial Chapter 14 Summary of how to implement the status feed
[Ruby on Rails] Try to create a service that makes local cats happy
[Ruby on Rails] How to Japaneseize the error message of Form object (ActiveModel)
[Ruby On Rails] Description that allows only specific users to transition to the edit page
[Ruby on Rails] How to write enum in Japanese