As the title suggests, this article is "How to update user information without entering a password". While creating an application in Rails, I wondered if I could somehow update my user information without having to enter my password. It seems that the update method requires you to enter the password. So, if you think about it, many web applications such as SNS and EC sites can be updated without entering a password, such as user name and profile. I thought that it would be easier than I expected without overriding the update method.
There was a method with that decent name, update_without_password. Using this, I implemented it as follows this time. The method to be used is simply sorted according to whether the password included in the parameter is empty.
#If the password contained in the parameter is empty if params[:password].blank? #Update user information without password @user.update_without_password(user_params) #If the parameter contains a password else @user.update(user_params)
If the model has the following description, an error will occur when using the update_without_password method. It's a validation that means that you must enter a password!
validates :password, presence: true
Let's specify the action to apply validation with the on option. If you do the following, validation will only be applied when you create the user.
validates :password, presence: true, on: :create