If you install a gem called devise, you can easily implement login and sign-up functions, Strong parameters (password, email address) are provided in advance.
However, when you log in or sign up, you may also want other strong parameters (name, image, etc.).
In that case, we will output how to add strong parameters.
You can use the devise_parameter_sanitizer method to add parameters to the strong parameters set in devise.
Example of usage
devise_parameter_sanitizer.permit(The type of method you want to add, keys: [Parameter name you want to add])
For example, if you need image information when signing up
devise_parameter_sanitizer.permit(:sign_up, keys: [:image])
You now have a devise that uploads images when you sign up. After that, there are a few things to keep in mind.
① Set the devise_parameter_sanitizer method to before_action
(2) The devise_parameter_sanitizer method is described in the controller that inherits the Devise controller.
③ Do not write devise_parameter_sanitizer method directly in before_action
that? What you say ① is different! ?? Actually, you can't set it directly before_action. Basically, devise_parameter_sanitizer is defined in "configure_permitted_parameters method". Then set configure_permitted_parameters to before_action.
If you're not sure, let's take a look at an example.
before_action :configure_permitted_parameters
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:image])
end
configure_permitted_parameters means to set the allowed parameters.
That's it!
Recommended Posts