I want to set devise_parameter_sanitizer individually when I create two devises

Create two devises and write the procedure when you want to set individual devise_parameter_sanitizer.

The original application is being created.

ruby '2.6.5' rails '6.0.0'

First, as a prerequisite, start with each devise's routing, model, controller, and view settings. Please refer to here for the above creation method.

Now, normally, if there is one devise for one application, use before_action in the application_controller.rb file to process devise_parameter_sanitizer.

app/controllers/application_controller.rb

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

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

In this way, you can allow a specific column when registering as a user.

This is how to set devise_parameter_sanitizer of different models when two devises are created.

This time I made two devises, user and doctor.

First of all, it's in the README of the repository, so I tried it, but it didn't work. I didn't know whether to create a file to inherit or to utilize an existing file.

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

For User
    app/controllers/users/registrations_controller.rb

For Doctor
    app/controllers/doctors/registrations_controller.rb

Find the following description in the large number of commented outs in the user file.

app/controllers/users/registrations_controller.rb


   #  before_action :configure_sign_up_params, only: [:create]

Enable comments for. . ↓

app/controllers/users/registrations_controller.rb


   before_action :configure_sign_up_params, only: [:create]

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

app/controllers/users/registrations_controller.rb


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

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

app/controllers/users/registrations_controller.rb


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

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

As some of 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 an individual file.

When doing the above

   TypeError

   superclass mismatch for class DoctorController

   class DoctorController < ApplicationController

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

   class DoctorController < ApplicationController
   class DoctorsController < ApplicationController

Since it inherits from app / controllers / doctors / registrations_controller.rb, there is no "" s "", so describe it.

Rails has strict naming conventions, so be careful! !!

Recommended Posts

I want to set devise_parameter_sanitizer individually when I create two devises
I want to create a generic annotation for a type
Rspec: I want to test the post-execution state when I set a method on subject
I want to randomly generate information when writing test code
I want to set the conditions to be displayed in collection_check_boxes
I want to convert characters ...
I want to create a form to select the [Rails] category
I want to create a Parquet file even in Ruby
I want to create a chat screen for the Swift chat app!
Links I want to help when JSF doesn't work at all
I want to avoid OutOfMemory when outputting large files with POI
[Rails] I want to add data to Params when transitioning with link_to
I want to notice that I forgot to specify arg when building Docker
Swift: I want to chain arrays
I want to use FormObject well
I want to convert InputStream to String
I want to docker-compose up Next.js!
I want to know the JSP of the open portlet when developing Liferay
[Active Admin] I want to customize the default create and update processing
I want to create a dark web SNS with Jakarta EE 8 with Java 11
I want to get the IP address when connecting to Wi-Fi in Java
I want to display an error message when registering in the database
I want to develop a web application!
I want to write a nice build.gradle
I want to eliminate duplicate error messages
I want to make an ios.android app
I want to display background-ground-image on heroku.
I want to use DBViewer with Eclipse 2018-12! !!
I want to RSpec even at Jest!
I want to write a unit test!
I want to install PHP 7.2 on Ubuntu 20.04.
I want to stop Java updates altogether
I set Ubuntu 20.04 to recognize MX Master 3
I want to use @Autowired in Servlet
I want to target static fields to @Autowired
I want to do team development remotely
[Android] I want to create a ViewPager that can be used for tutorials
[Rails / JavaScript / Ajax] I tried to create a like function in two ways.
I want to create the strongest local development environment using VSCode Remote Containers