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.
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 |
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
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
https://qiita.com/senou/items/f1491e53450cb347606b
Recommended Posts