Routing settings from file creation when creating multiple devises and creating separate login screens

We are developing original applications. I will summarize what I learned from creating applications.

Create multiple models with user and doctor, and create separate new registration screens and login screens. Normally, since it is one login screen, routine settings etc. are automatically generated, but if you make two, you will need to customize and summarize the difficulties.

ruby '2.6.5' rails '6.0.0'

First install devise.

  gem 'devise'

Install it.

  % bundle install

Reboot to reflect the Gem.

  % rails s

In order to use devise, in addition to installing Gem, it is necessary to create a configuration file with a command dedicated to devise, so use the command to automatically generate "files used for configuration".

   % rails g devise:install

Here is a different customization than usual.

1. Tweak config / initializers / devise.rb.

There is config / initializers / devise.rb in the file generated by rails g devise: install. Of that file

# config.scoped_views = false

this

 config.scoped_views = true

Uncomment and change to. It's hard to understand, but I think it's on line 247.

2. Create a model related to each devise.

   % rails g devise user
   % rails g devise doctor

Multiple files will be generated for each.

3. Make a controller related to each model.

   % rails g devise:controllers users
   % rails g devise:controllers doctors

Make it controllers (plural) and name it (plural).

4. Create a view associated with each devise.

   % rails g devise:views users
   % rails g devise:views doctors

You have now created the MVC devise related files.

So far, it's almost similar to creating one devise.

5. Next, create the routing.

   Rails.application.routes.draw do
  devise_for :doctors, controllers: {
    sessions: 'doctors/sessions',
    passwords: 'doctors/passwords',
    registrations: 'doctors/registrations'
  }
  devise_for :users, controllers: {
    sessions: 'users/sessions',
    passwords: 'users/passwords',
    registrations: 'users/registrations'  
  }

In this way, make sure that each routing does not overlap.

Looking at the routing,


          Prefix Verb   URI Pattern                                                                              Controller#Action
                   new_doctor_session GET    /doctors/sign_in(.:format)                                                               doctors/sessions#new
                       doctor_session POST   /doctors/sign_in(.:format)                                                               doctors/sessions#create
               destroy_doctor_session DELETE /doctors/sign_out(.:format)                                                              doctors/sessions#destroy
                  new_doctor_password GET    /doctors/password/new(.:format)                                                          doctors/passwords#new
                 edit_doctor_password GET    /doctors/password/edit(.:format)                                                         doctors/passwords#edit
                      doctor_password PATCH  /doctors/password(.:format)                                                              doctors/passwords#update
                                      PUT    /doctors/password(.:format)                                                              doctors/passwords#update
                                      POST   /doctors/password(.:format)                                                              doctors/passwords#create
           cancel_doctor_registration GET    /doctors/cancel(.:format)                                                                doctors/registrations#cancel
              new_doctor_registration GET    /doctors/sign_up(.:format)                                                               doctors/registrations#new
             edit_doctor_registration GET    /doctors/edit(.:format)                                                                  doctors/registrations#edit
                  doctor_registration PATCH  /doctors(.:format)                                                                       doctors/registrations#update
                                      PUT    /doctors(.:format)                                                                       doctors/registrations#update
                                      DELETE /doctors(.:format)                                                                       doctors/registrations#destroy
                                      POST   /doctors(.:format)                                                                       doctors/registrations#create
                     new_user_session GET    /users/sign_in(.:format)                                                                 users/sessions#new
                         user_session POST   /users/sign_in(.:format)                                                                 users/sessions#create
                 destroy_user_session DELETE /users/sign_out(.:format)                                                                users/sessions#destroy
                    new_user_password GET    /users/password/new(.:format)                                                            users/passwords#new
                   edit_user_password GET    /users/password/edit(.:format)                                                           users/passwords#edit
                        user_password PATCH  /users/password(.:format)                                                                users/passwords#update
                                      PUT    /users/password(.:format)                                                                users/passwords#update
                                      POST   /users/password(.:format)                                                                users/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

It will be. User and doctor can be routed separately.

In form_with, user was able to go to the new registration screen and login screen, but I wrote an article because the routing of doctor could not be done well.

Recommended Posts

Routing settings from file creation when creating multiple devises and creating separate login screens