Rails のルーティング
ルーティングを追加するとき、毎回調べているので少しずつまとめていこうかと思います。 等価な書き方が色々あるので絶対これというわけではありません。
scope
URI Pattern にのみ追加されます。
Rails.application.routes.draw do scope :user do resources :profile end end
➜ sample git:(master) ✗ be rake routes
Prefix Verb URI Pattern Controller#Action
profile_index GET /user/profile(.:format) profile#index
POST /user/profile(.:format) profile#create
new_profile GET /user/profile/new(.:format) profile#new
edit_profile GET /user/profile/:id/edit(.:format) profile#edit
profile GET /user/profile/:id(.:format) profile#show
PATCH /user/profile/:id(.:format) profile#update
PUT /user/profile/:id(.:format) profile#update
DELETE /user/profile/:id(.:format) profile#destroy
scope module:
Controller#Action にのみ追加されます。
Rails.application.routes.draw do scope module: :user do resources :profile end end
➜ sample git:(master) ✗ be rake routes
Prefix Verb URI Pattern Controller#Action
profile_index GET /profile(.:format) user/profile#index
POST /profile(.:format) user/profile#create
new_profile GET /profile/new(.:format) user/profile#new
edit_profile GET /profile/:id/edit(.:format) user/profile#edit
profile GET /profile/:id(.:format) user/profile#show
PATCH /profile/:id(.:format) user/profile#update
PUT /profile/:id(.:format) user/profile#update
DELETE /profile/:id(.:format) user/profile#destroy
scope as:
Prefix にのみ追加されます。
Rails.application.routes.draw do scope as: :user do resources :profile end end
➜ sample git:(master) ✗ be rake routes
Prefix Verb URI Pattern Controller#Action
user_profile_index GET /profile(.:format) profile#index
POST /profile(.:format) profile#create
new_user_profile GET /profile/new(.:format) profile#new
edit_user_profile GET /profile/:id/edit(.:format) profile#edit
user_profile GET /profile/:id(.:format) profile#show
PATCH /profile/:id(.:format) profile#update
PUT /profile/:id(.:format) profile#update
DELETE /profile/:id(.:format) profile#destroy
scope と module: と as:
Prefix と URI Pattern と Controller#Action 全てに追加されます。
Rails.application.routes.draw do scope :user, module: :user, as: :user do resources :profile end end
➜ sample git:(master) ✗ be rake routes
Prefix Verb URI Pattern Controller#Action
user_profile_index GET /user/profile(.:format) user/profile#index
POST /user/profile(.:format) user/profile#create
new_user_profile GET /user/profile/new(.:format) user/profile#new
edit_user_profile GET /user/profile/:id/edit(.:format) user/profile#edit
user_profile GET /user/profile/:id(.:format) user/profile#show
PATCH /user/profile/:id(.:format) user/profile#update
PUT /user/profile/:id(.:format) user/profile#update
DELETE /user/profile/:id(.:format) user/profile#destroy
namespace
Prefix と URI Pattern と Controller#Action 全てに追加されます。
( scope と module: と as: ) と同じ結果になります。
Rails.application.routes.draw do namespace :user do resources :profile end end
➜ sample git:(master) ✗ be rake routes
Prefix Verb URI Pattern Controller#Action
user_profile_index GET /user/profile(.:format) user/profile#index
POST /user/profile(.:format) user/profile#create
new_user_profile GET /user/profile/new(.:format) user/profile#new
edit_user_profile GET /user/profile/:id/edit(.:format) user/profile#edit
user_profile GET /user/profile/:id(.:format) user/profile#show
PATCH /user/profile/:id(.:format) user/profile#update
PUT /user/profile/:id(.:format) user/profile#update
DELETE /user/profile/:id(.:format) user/profile#destroy