[Ruby] I want to set devise_parameter_sanitizer individually when creating two devises

1 minute read

Write the procedure for creating two devises and setting individual devise_parameter_sanitizer.

Original application is being created.

ruby ‘2.6.5’ rails ‘6.0.0’

First of all, as a prerequisite, we will start with the settings of routing, model, controller, view of each devise. Please refer to here for the above creation method.

By the way, if there is usually one devise for one application, the process of devise_parameter_sanitizer is performed using before_action in the application_controller.rb file.


   class ApplicationController <ActionController::Base
  before_action :configure_permitted_parameters, if: :devise_controller?

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:nickname])

In this way you can allow specific columns when registering a user.

This is how to set devise_parameter_sanitizer for different models when creating two devises.

This time, I made two devise, user and doctor.

First, I tried it because it is in the README of the repository, but it does not come out quite right. I didn’t know whether to make a file to inherit or to leverage an existing file.

As a result, the individual file is app/controllers/users/registrations_controller.rb, so use that.

For User,

    For Doctor,

Find the following description from the large number of commented out contents of the user file.


   # before_action :configure_sign_up_params, only: [:create]

Enable comments for. . ↓


   before_action :configure_sign_up_params, only: [:create]

Now, when creating a user, configure_sign_up_params is now called, so In the same file, def configure_sign_up_params Comment out and enable.


   def configure_sign_up_params
     devise_parameter_sanitizer.permit(:sign_up, keys: [:nickname])

Then write the karamu you want to add to this part of (:sign_up, keys: [:nickname]).


 def configure_sign_up_params
    devise_parameter_sanitizer.permit(:sign_up, keys: [:nickname, :password])

Doctor will do the same work. The file is app/controllers/doctors/registrations_controller.rb.

As you may have noticed, devise_parameter_sanitize is not written in app/controllers/application_controller.rb because it is not defined in the application controller and is called by before_action in a separate file.

When doing the above


Superclass mismatch for class DoctorController

   class DoctorController <ApplicationController

I got an error. If you look closely, class DoctorController <ApplicationController is Doctor.

```before change class DoctorController <ApplicationController

``` after change
   class DoctorsController <ApplicationController

Since it inherits to app/controllers/doctors/registrations_controller.rb, there is no “”s”, so describe it.

Note that rails has a strict naming convention! !!