[RUBY] [Rails] Add strong parameters to devise

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.

Use devise_parameter_sanitizer!

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.

Precautions when using devise_parameter_sanitizer

① 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])

configure_permitted_parameters means to set the allowed parameters.

That's it!

Recommended Posts

[Rails] Add strong parameters to devise
About rails strong parameters
[rails] What are Strong Parameters?
Enable strong parameters in devise
[Rails] How to install devise
Pass parameters to Rails link_to
[Rails] How to get the contents of strong parameters
[Rails] How to use gem "devise"
[Rails] How to use devise (Note)
[Rails] devise
[Strong parameters]
[Rails] Add a confirmation screen and a completion screen to devise membership registration.
I want to add devise in Rails, but I can't bundle install
Migration file to add comment to Rails table
[Rails] [Memo] When to add = to <%%> and when not
[Note] How to use Rails 6 Devise + cancancan
[Rails] How to translate devise into Japanese
Add binding.pry (rails)
[Rails] Introducing devise
rails + devise + devise_token_auth
[Rails] Add page nation to table [For beginners]
The road to Japaneseizing Rails devise error messages
How to add / remove Ruby on Rails columns
Add a tag function to Rails. Use acts-as-taggable-on
How to conditionally add html.erb class in Rails
One way to redirect_to with parameters in rails
[Rails] Added in devise: username not added to database
Add files to jar files
[rails] How to use devise helper method before_action: authenticate_user!
How to write Rails
Introducing CircleCI to Rails
[Rails] devise helper method
[Rails] Customize devise validation
Introduce devise in Rails to implement user management functionality
Introducing Bootstrap to Rails !!
Summary of strong parameters
Introduce Vue.js to Rails
Handle devise with Rails
How to uninstall Rails
[Rails] devise introduction method
[Rails] How to edit and customize devise view and controller
[Rails 6] Add images to seed files (using Active Storage)
[Rails] Add Tags that are related to has_many to Devise User (addition of function to follow tags)
[rails] About devise defaults
[Rails] Processing after adding a column to the devise table
Add an icon to the header link using Rails fontawesome
I was addicted to setting default_url_options with Rails devise introduction
[Ruby] Strong parameters [require] [permit] [merge] How to use Role
[Rails] How to delete images uploaded by carrierwave (using devise)
Super easy in 2 steps! How to install devise! !! (rails 5 version)
[Rails5] japanMap link How to write parameters in js.erb file
[Rails] How to write user_id (foreign key) in strong parameter
Add -parameters option to javac to get argument names by reflection
[rails] How to post images
[Rails] Introduction of devise Basics
What is Rails gem devise?
How to add ActionText function
[Rails] gem devise installation flow
[Rails] How to use enum
4 Add println to the interpreter
How to use rails join