Good evening Aloha man, Yasunori!
Today I would like to talk about the model validation that I was addicted to while creating the portfolio! !!
Maybe I will be myself tomorrow ...?
Validation of User model password
on: :create
Let's use!
What do you mean ...? I think that it will be, so I will explain it step by step.
First of all, in the portfolio, when a user is registered as a member, a function to have a dedicated My Page and to post reviews was created with the Rails Gem devise
.
Among such functions, an incident occurred on ** "My user information change function page" ** that can be done on My Page ...
The user information change page is a page where you can change the user name, icon image, etc., but if you are using devise
, you usually need to enter the password every time you change it.
However, for those who say, "It's a hassle to enter the password every time with a small change ...", devise
provides a method that makes it unnecessary to enter the password. ..
registrations_controller.rb
protected
def update_resource(resource, params)
resource.update_without_password(params)
end
Initially, this method was commented out, so uncommenting it should have eliminated the need to enter a password when updating user information.
However, when I actually enter the changes and press the update button
...
"Oh ... why !? I was asked to enter my password ...!?"
Of course, in this situation, if you enter the password and confirmation password and press the update button
, the user information will be updated ...
What I wanted was such an unfamiliar feature! !!
So, after investigating various things, the cause was found.
You may have noticed that you have a good idea ...
Yes, the cause of this event was the validation of the password set in the User model
! !!
What this means is that this time as password validation
presence: true(Option not to be registered if column is empty)
I was setting, but the part that says cannot be registered if the column is empty
has worked ... !!
In other words
User model "Oh, I'll update the user information ~" ↓ User model "You can also set the devise controller so that you don't need a password." ↓ User model "Oh, but I need a password to validate the model ..." ↓ User model "Yes, please enter your password too"
It seems that it was a flow of saying ... What ...
Once you know the cause, the rest is easy. All you have to do is reset the validation applied to the password so that it will be applied only when you register as a user.
So I showed you at the beginning,
on: :create #Option to work only during controller create action
will become necessary!
If you write in my code this time
user.rb
with_options presence: true do
#Validation is applied only when a new user is registered.
with_options on: :create do
validates :password
validates :password_confirmation
end
end
Now, the user information is updated safely! Congratulations
I think that user functions and password validation are functions that are required by a relatively large number of apps, so if you have similar problems, please refer to them!
We would love to hear your suggestions and advice on this matter, so we welcome your comments! !!
Recommended Posts