Railsのルーティングの指定方法はいろいろな書き方がありますが、毎回どれを使うとどういうURLになるか調べることが多いので書き方一覧をまとめました。
各書き方別URL
以下、使用する単語の前提
URL = ブラウザ上で表示されるURL
パス = ソースコード上で user_path
などの表記に使われる名前
ノーマルな書き方
get 'sample' => 'sample#index'
post 'sample_create' => 'sample#create'
patch 'sample_update' => 'sample#update'
delete 'sample_delete' => 'sample#destroy'
$ bin/rails routes |grep sample
=>
sample GET /sample(.:format) sample#index
sample_create POST /sample_create(.:format) sample#create
sample_update PATCH /sample_update(.:format) sample#update
sample_delete DELETE /sample_delete(.:format) sample#destroy
resource :sample
$ bin/rails routes |grep sample
=>
edit_sample GET /sample/edit(.:format) samples#edit
sample GET /sample(.:format) samples#show
PATCH /sample(.:format) samples#update
PUT /sample(.:format) samples#update
DELETE /sample(.:format) samples#destroy
POST /sample(.:format) samples#create
resources :samples
$ bin/rails routes |grep sample
=>
samples GET /samples(.:format) samples#index
POST /samples(.:format) samples#create
new_sample GET /samples/new(.:format) samples#new
edit_sample GET /samples/:id/edit(.:format) samples#edit
sample GET /samples/:id(.:format) samples#show
PATCH /samples/:id(.:format) samples#update
PUT /samples/:id(.:format) samples#update
DELETE /samples/:id(.:format) samples#destroy
memberとcollection
CRUD以外のURLを作るときのオプションで、memberはIDのパラメータを必要とする。collectionはIDがいらない。
resources :sample do
get 'user_index', on: :collection
get 'user_show', on: :member
end
$ bin/rails routes |grep user
=>
user_index_sample_index GET /sample/user_index(.:format) sample#user_index
user_show_sample GET /sample/:id/user_show(.:format) sample#user_show
namespace
パス、URL、ツリー構造全て変える
namespace :sample do
resources :users
end
$ bin/rails routes |grep sample
=>
sample_user_index GET /sample/users(.:format) sample/users#index
POST /sample/users(.:format) sample/users#create
new_sample_user GET /sample/users/new(.:format) sample/users#new
edit_sample_user GET /sample/users/:id/edit(.:format) sample/users#edit
sample_user GET /sample/users/:id(.:format) sample/users#show
PATCH /sample/users/:id(.:format) sample/users#update
PUT /sample/users/:id(.:format) sample/users#update
DELETE /sample/users/:id(.:format) sample/users#destroy
scope
パス、ツリー構造は変えずにURLだけ変える
scope :sample do
resources :users
end
$ bin/rails routes |grep sample
=>
users GET /sample/users(.:format) users#index
POST /sample/users(.:format) users#create
new_user GET /sample/users/new(.:format) users#new
edit_user GET /sample/users/:id/edit(.:format) users#edit
user GET /sample/users/:id(.:format) users#show
PATCH /sample/users/:id(.:format) users#update
PUT /sample/users/:id(.:format) users#update
DELETE /sample/users/:id(.:format) users#destroy
module
パスやURLは変えずにツリー構造だけ変える
scope module: :sample do
resources :users
end
$ bin/rails routes |grep sample
=>
users GET /users(.:format) sample/users#index
POST /users(.:format) sample/users#create
new_user GET /users/new(.:format) sample/users#new
edit_user GET /users/:id/edit(.:format) sample/users#edit
user GET /users/:id(.:format) sample/users#show
PATCH /users/:id(.:format) sample/users#update
PUT /users/:id(.:format) sample/users#update
DELETE /users/:id(.:format) sample/users#destroy
pathオプション
URLを任意のものに変える
namespace :sample, path: '/' do
resources :users
end
$ bin/rails routes |grep sample
=>
sample_users GET /users(.:format) sample/users#index
POST /users(.:format) sample/users#create
new_sample_user GET /users/new(.:format) sample/users#new
edit_sample_user GET /users/:id/edit(.:format) sample/users#edit
sample_user GET /users/:id(.:format) sample/users#show
PATCH /users/:id(.:format) sample/users#update
PUT /users/:id(.:format) sample/users#update
DELETE /users/:id(.:format) sample/users#destroy
asオプション
パス(名前)を任意のものに変える
resources :sample, as: :user
$ bin/rails routes |grep sample
=>
user_index GET /sample(.:format) sample#index
POST /sample(.:format) sample#create
new_user GET /sample/new(.:format) sample#new
edit_user GET /sample/:id/edit(.:format) sample#edit
user GET /sample/:id(.:format) sample#show
PATCH /sample/:id(.:format) sample#update
PUT /sample/:id(.:format) sample#update
DELETE /sample/:id(.:format) sample#destroy
shallowオプション
親のIDを省略する
※オプションをつけないと show
update
など親のIDが必要になる部分が省略できます。
resources :sample, shallow: true do
resources :users
end
$ bin/rails routes |grep sample
=>
sample_users GET /sample/:sample_id/users(.:format) users#index
POST /sample/:sample_id/users(.:format) users#create
new_sample_user GET /sample/:sample_id/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
コメント