[Ruby] Creating multiple devises and creating a separate login screen from file creation to routing settings

2 minute read

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

Create multiple models with user and doctor, and create new registration screen and login screen separately. Normally, since it is one login screen, routine settings etc. are automatically generated, but if you make two, customization will be necessary and I will 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 Gem installation, it is necessary to create a configuration file with devise-specific commands, so a command to automatically generate a “file to be used for configuration” is used.

   % rails g devise:install

It’s unusual customization from here.

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.

   % rails g devise user
   % rails g devise doctor

Multiple files are generated for each.

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

Set it to controllers (plural) and name it (plural).

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

Now you have created the devise related files for MVC.

Up to this point, it’s almost similar to creating a normal 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, each route should not be covered.

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#edituser_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

となります。userとdoctorのルーティングが別々のものが出来ます。

form_withでuserは新規登録画面とログイン画面いけたのですが、doctorのルーティングがうまく出来なっかったので記事にしました。