blue workbench

Ruby and Rails

【Rails】deviseのルーティングをカスタマイズする

ノーマル

Rails.application.routes.draw do
  devise_for :users
end
$ 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
                    new_user_password GET    /users/password/new(.:format)                                                            devise/passwords#new
                   edit_user_password GET    /users/password/edit(.:format)                                                           devise/passwords#edit
                        user_password PATCH  /users/password(.:format)                                                                devise/passwords#update
                                      PUT    /users/password(.:format)                                                                devise/passwords#update
                                      POST   /users/password(.:format)                                                                devise/passwords#create
             cancel_user_registration GET    /users/cancel(.:format)                                                                  devise/registrations#cancel
                new_user_registration GET    /users/sign_up(.:format)                                                                 devise/registrations#new
               edit_user_registration GET    /users/edit(.:format)                                                                    devise/registrations#edit
                    user_registration PATCH  /users(.:format)                                                                         devise/registrations#update
                                      PUT    /users(.:format)                                                                         devise/registrations#update
                                      DELETE /users(.:format)                                                                         devise/registrations#destroy
                                      POST   /users(.:format)                                                                         devise/registrations#create
                new_user_confirmation GET    /users/confirmation/new(.:format)                                                        devise/confirmations#new
                    user_confirmation GET    /users/confirmation(.:format)                                                            devise/confirmations#show
                                      POST   /users/confirmation(.:format)                                                            devise/confirmations#create

結局こうした

Rails.application.routes.draw do
  # ルーティングをカスタマイズする場合は、まず skip でルーティング生成をスキップする
  devise_for :users, skip: [:sessions, :registrations], controllers: {
      registrations: 'users/registrations'
  }

  # skip したルーティングをここでカスタマイズする
  devise_scope :user do
    get 'sign_in', to: 'devise/sessions#new', as: :new_user_session
    post 'sign_in', to: 'devise/sessions#create', as: :user_session
    delete 'sign_out', to: 'devise/sessions#destroy', as: :destroy_user_session

    # コントローラをカスタマイズした場合は、以下もカスタマイズしたほうを見るようにする
    get 'sign_up', to: 'users/registrations#new', as: :new_user_registration
    post 'sign_up', to: 'users/registrations#create', as: :user_registration
  end
end
$ rails routes
                               Prefix Verb   URI Pattern                                                                              Controller#Action
                    new_user_password GET    /users/password/new(.:format)                                                            devise/passwords#new
                   edit_user_password GET    /users/password/edit(.:format)                                                           devise/passwords#edit
                        user_password PATCH  /users/password(.:format)                                                                devise/passwords#update
                                      PUT    /users/password(.:format)                                                                devise/passwords#update
                                      POST   /users/password(.:format)                                                                devise/passwords#create
                new_user_confirmation GET    /users/confirmation/new(.:format)                                                        devise/confirmations#new
                    user_confirmation GET    /users/confirmation(.:format)                                                            devise/confirmations#show
                                      POST   /users/confirmation(.:format)                                                            devise/confirmations#create
                     new_user_session GET    /sign_in(.:format)                                                                       devise/sessions#new
                         user_session POST   /sign_in(.:format)                                                                       devise/sessions#create
                 destroy_user_session DELETE /sign_out(.:format)                                                                      devise/sessions#destroy
                new_user_registration GET    /sign_up(.:format)                                                                       users/registrations#new
                    user_registration POST   /sign_up(.:format)                                                                       users/registrations#create

コントローラをカスタマイズする

Rails.application.routes.draw do
  devise_for :users, controllers: {
      registrations: 'users/registrations'
  }
end
$ 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
                    new_user_password GET    /users/password/new(.:format)                                                            devise/passwords#new
                   edit_user_password GET    /users/password/edit(.:format)                                                           devise/passwords#edit
                        user_password PATCH  /users/password(.:format)                                                                devise/passwords#update
                                      PUT    /users/password(.:format)                                                                devise/passwords#update
                                      POST   /users/password(.:format)                                                                devise/passwords#create
             cancel_user_registration GET    /users/cancel(.:format)                                                                  users/registrations#cancel
                new_user_registration GET    /users/sign_up(.:format)                                                                 users/registrations#new
               edit_user_registration GET    /users/edit(.:format)                                                                    users/registrations#edit
                    user_registration PATCH  /users(.:format)                                                                         users/registrations#update
                                      PUT    /users(.:format)                                                                         users/registrations#update
                                      DELETE /users(.:format)                                                                         users/registrations#destroy
                                      POST   /users(.:format)                                                                         users/registrations#create
                new_user_confirmation GET    /users/confirmation/new(.:format)                                                        devise/confirmations#new
                    user_confirmation GET    /users/confirmation(.:format)                                                            devise/confirmations#show
                                      POST   /users/confirmation(.:format)                                                            devise/confirmations#create

スキップする

Rails.application.routes.draw do
  devise_for :users, skip: [:sessions, :registrations]
end
$ rails routes
                               Prefix Verb   URI Pattern                                                                              Controller#Action
                    new_user_password GET    /users/password/new(.:format)                                                            devise/passwords#new
                   edit_user_password GET    /users/password/edit(.:format)                                                           devise/passwords#edit
                        user_password PATCH  /users/password(.:format)                                                                devise/passwords#update
                                      PUT    /users/password(.:format)                                                                devise/passwords#update
                                      POST   /users/password(.:format)                                                                devise/passwords#create
                new_user_confirmation GET    /users/confirmation/new(.:format)                                                        devise/confirmations#new
                    user_confirmation GET    /users/confirmation(.:format)                                                            devise/confirmations#show
                                      POST   /users/confirmation(.:format)                                                            devise/confirmations#create