The method that came out when I was copying sutras with Rails became "what this", so I will review it again and write it as an output and a memorandum.
devise_parameter_sanitizer is a method provided by the gem called devise. It is a method to get parameters (name, email, etc.) from requests such as login and new registration when implementing user management function.
Parameters are externally passed data included in the request. It is processed by the controller and registered as data in the columns of the table through the model. Is it something like data that is input from the input field of the browser and sent?
To enter data in the form and submit it as a request, write the following description.
index.html
#This is the story of the sender
<%= form_with url:”URL”, method: :post, local: true do |form| %>
<%= form.text_field :content %>
<% end %>
I think it's something that lets you fill out a form and send data to a URL. The ": content" part of the second line becomes params, and as "params [: content]" It is the data passed from the outside to the controller.
This was the story of the sender, but I would like to think about the recipient as well. The recipient can be restricted to receive only parameters with the specified key (column name). This is called a strong parameter. The description is as follows.
players_controller.rb
#This is the story of the recipient
params.require(:player).permit(:name, :age)
In the above description, only the data of the key (column) "name" and the key (column) "age" of the Player model will be received from the request. Specify the table with columns in require. If the request contains "height", "speed", and "defence" data in addition to the "name" and "age" keys, they will not be received.
How to describe is as follows.
application_controller.rb
#For devise
devise_parameter_sanitizer.permit(:sign_up, keys: [:email])
Since devise_parameter_sanitizer will be a method of devise, the model will be the User model created by devise, so the above require will not be described. The parameter (data) you get when you sign up means "email".
When I wrote the code with this devise_parameter_sanitizer, I thought that the devise gem is quite convenient for implementing the login function. You can register and log in without creating a login function from scratch. By putting the necessary information (column) in devise_parameter_sanitizer, devise will do the rest to some extent, so I'm not sure yet, but I feel that it is a very convenient method.
Although I wrote this article, there are still some things I don't understand. I would be grateful if you could tell me if there is something wrong with this recognition. Thank you.
An active engineer explains how to use Rails' require / permit method [for beginners] https://techacademy.jp/magazine/22078
Recommended Posts