Strong parameter sanitizer method required to customize devise's "new registration", "login", and "update information" functions
It has become a long title, but the point is about "devise user management function". You can easily create user management functions using devise, but various settings are required to change the default functions. This time, we will introduce the processing required when customizing "new registration", "login", and "update information".
I have used devise I want to implement something other than the default of devise Understand strong parameters
ruby 2.6.5 rails 6.0.0 devise 4.7.3
First of all, the conclusion is as follows.
app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
#If it's a devise controller, load the method before every action
before_action :configure_permitted_parameters, if: :devise_controller? #About login function
before_action :configure_account_update_parameters, if: :devise_controller? #About editing function
private
#About login function
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:nickname, :family_name_kanji, :first_name_kanji, :birthday])
end
#About editing function
def configure_account_update_parameters
devise_parameter_sanitizer.permit(:account_update, keys: [:nickname, :family_name_kanji, :first_name_kanji, :birthday])
end
end
First, I will explain the method.
When you want to customize "new registration", "login", and "update information" other than the default.
The sanitizer method is a method to get the parameters related to devise's User model. There is a description inside the device to get the parameters by default, but it is to apply a disinfectant = (sanitizer) to it so that it can be customized.
Then combine the sanitizer and permit methods. That way, you can include your own newly added columns for the strong parameters defined by default in devise.
There are two types of permit methods, so I will derail it, but I will explain it a little. The "sanitizer" permit is a method for getting the parameters of devise. It is different from the permit method of params used when saving with the controller of "Rails".
Below are examples of each.
controller.rb
#Example)permit method of params
params.require(:Model name).permit(:Keys you want to allow)
app/controllers/application_controller.rb
#Example) devise_parameter_sanitizer permit method
devise_parameter_sanitizer.permit(:devise process name, keys: [:Key (column name) you want to allow]
The method name is customarily defined as configure_permitted_parameters, but you can name it freely.
app/controllers/application_controller.rb
private
def configure_permitted_parameters #You can name the method freely.
#Allow parameters for devise User model
devise_parameter_sanitizer.permit(:devise process name, keys: [:Key (column name) you want to allow])
end
This is the explanation of the devise_parameter_sanitizer method.
As mentioned in "2.1 Timing of use", the description of ": devise process name" changes for the purpose of processing. The purpose of processing is "new registration", "login", and "update information".
Process name | Purpose |
---|---|
:sign_in | Login(Sign in)When processing |
:sign_up | sign up(Sign up)When processing |
:account_update | When processing account information update |
"Where to write" is "a file inherited by all controllers". That is, the application_controller.rb file. After this controller is loaded, all other controllers will be loaded. Also, since we want to process all actions before processing, we use before_action.
app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
#↑ This#
private
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:nickname])
#↑(:devise process name, keys: [:Key (column name) you want to allow])
end
end
This completes most of the implementation. All you have to do is edit the permit according to what you want to do. The above is devise processing name =: sign_up keys = [:nickname] It is implemented in.
The point of the procedure is Described in application_controller.rb set before_action Use sanitizer.permit method
That's all about the strong parameter sanitizer method required to customize the "new registration", "login", and "update information" functions of devise.
I am a beginner in programming, but I am posting an article in the hope that it will help people who are similarly troubled. See you next time ~