deviseも便利な機能が故、それぞれ独自のメソッドが自動で生成してくれるルーティングについて、こんがらがってしまいます。
そんな同じお悩みの方はぜひ。
#devise_for
routes.rb
devise_for :users
$ rails routes
Prefix Verb URI Pattern Controller#Action
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
###resources :usersとの比較
アクション名 | リクエスト | resources | devise_for |
---|---|---|---|
new | GET | /users/new | /users/sign_up(.:format) |
edit | GET | /users/:id/edit | /users/edit(.:format) |
show | GET | /users/:id | なし |
index | GET | /users | なし |
create | POST | /users | /users(.:format) |
update | PATCH / PUT | /users | /users(.:format) |
destroy | DELETE | /users | /users(.:format) |
#devise_scope
routes.rb
devise_scope :user do
get 'signin' => 'devise_token_auth/sessions#new'
post 'signin' => 'devise_token_auth/sessions#create'
post 'signup' => 'users#create'
put 'update' => 'users#update'
end
$ rails routes
Prefix Verb URI Pattern Controller#Action
signin GET /signin(.:format) devise_token_auth/sessions#new
POST /signin(.:format) devise_token_auth/sessions#create
signup POST /signup(.:format) users#create
update PUT /update(.:format) users#update
#mount_devise_token_auth_for
routes.rb
mount_devise_token_auth_for 'User', controllers: {
registrations: 'users'
}
$ rails routes
Prefix Verb URI Pattern Controller#Action
signin GET /signin(.:format) devise_token_auth/sessions#new
POST /signin(.:format) devise_token_auth/sessions#create
signup POST /signup(.:format) users#create
update PUT /update(.:format) users#update