It was surprisingly easy. How to update user information without entering a password

Overview

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.

What I actually did

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)

important point

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!

app/models/user.rb

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

Recommended Posts

It was surprisingly easy. How to update user information without entering a password
How to update devise user information without a password
How to update user edits in Rails Devise without entering a password
How to give Spring Security authenticated user information other than userId and password and how to refer to it
How to create a class that inherits class information
How to make a cache without thinking too much
[Ruby/Rails] How to generate a password in a regular expression
[iOS] [Objective-C] How to update a widget from an Objective-C app
How to output array values without using a for statement
How to join a table without using DBFlute and sql
A memo that was unexpectedly difficult even though it seemed easy to exchange between two Entity