[RUBY] [Rails] Summary of complicated routing configurations

This time, we will focus on the ʻUsers resource and look at the pattern of adding the ʻAPI directory as an additional configuration and setting the path state when the sort method is added as a new method.

Simplified chart

type path class File Path
scope /api/users UsersController /users_controller.rb
namespace /api/users Api::UsersController /api/users_controller.rb
module /users Api::UsersController /api/users_controller.rb
member /users/:id/sort
collection /users/sort

Controllers directory structure and routing

scope

routes.rb


# /api/users

scope :api do
  resources :users
end

Directory structure


/controllers--
            |
            |--application.rb
            |
            |--users_controller.rb

controller.rb


class UsersController < ApplicationController
end

namespace

routes.rb


# /api/users

namespace :api do
  resources :users
end

Directory structure


controllers/-
            |
            |--application.rb
            |
            |--api/-
                   |
                   |--users_controller.rb

controller.rb


class Api::UsersController < ApplicationController
end

module

routes.rb


# /users

scope module: :api do
  resources :users
end

Directory structure


controllers/-
            |
            |--application.rb
            |
            |--api/-
                   |
                   |--users_controller.rb

controller.rb


class Api::UsersController < ApplicationController
end

Make a whole new path

member

routes.rb


#Path with id/users/:id/sort

resources :users do
  member do
    get :sort
  end
end

Abbreviation

routes.rb


resources :users do
  get :sort, on: :member
end

collection

routes.rb


#Path without id/users/sort

resources :users do
  collection do
    get :sort
  end
end

Abbreviation

routes.rb


resources :users do
  get :sort, on: :collection
end

Reference article

https://qiita.com/senou/items/f1491e53450cb347606b

Recommended Posts

[Rails] Summary of complicated routing configurations
Rails 6.0 Routing Summary
[Rails] devise-related routing summary
[Rails Struggle/Rails Tutorial] Summary of Rails Tutorial Chapter 2
Summary of rails validation (for myself)
[Rails Struggle/Rails Tutorial] Summary of Heroku commands
About Rails routing
Rails Routing Basics
rails db: 〇〇 Summary
Summary of basic knowledge of Rails acquired by progate
[Note] Summary of rails login function using devise ①
Summary of frequently used commands in Rails and Docker
[Rails] Migration command summary
A brief summary of Rails association options (foreign_key, primary_key)
Summary of OpenJDK sources
Summary of strong parameters
Summary of jar files
Rails Tutorial/Significance of Indexing
[Rails] rails db command summary
Summary of information security
Summary of using FragmentArgs
rails error resolution summary
[Note] Rails3 routing confirmation
Understand Rails "shallow" routing
[Rails] Complete routing settings
Summary of using DBFlow
Summary of Java support 2018
Ruby on Rails for beginners! !! Summary of new posting functions
Summary of initial work when creating an app with Rails
Rails implementation of ajax removal
Summary of FileInputStream and BufferedInputStream
[Java11] Stream Summary -Advantages of Stream-
Summary of using Butter Knife
Utilization of Rails Boolean type
[Java] Summary of regular expressions
[Rails 6] Implementation of search function
[Java] Summary of operators (operator)
[Rails] Implementation of category function
Organize Rails routing using draw
Rails Tutorial (4th Edition) Summary
Summary of "abstract interface differences"
Summary of Java language basics
[Java] Summary of for statements
Summary of Java Math class
Rails routing controller view relationship
[Rails] Implementation of tutorial function
How to write Rails routing
[Rails] Implementation of like function
Summary of basic functions of ImageJ
Summary of 2020 programming learning output
Ruby on Rails validation summary
[Java] Summary of control syntax
Summary of java error processing
[Java] Summary of design patterns
Introduction to RSpec-Everyday Rails Summary-
Rails singular resource routing by resource
[Java] Summary of mathematical operations
[Webpacker] Summary of how to install Bootstrap and jQuery in Rails 6.0
[Ruby on Rails] "|| =" ← Summary of how to use this assignment operator