[RUBY] A story I was addicted to in Rails validation settings

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 ...?

Conclusion

Validation of User model password

on: :create

Let's use!

The situation when I was addicted

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 ...

What happened?

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 ...

スクリーンショット_2020-12-22_22_48_09.jpg

"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.

Cause

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 ...

solution

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

Finally

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

A story I was addicted to in Rails validation settings
[Circle CI] A story I was addicted to at Start Building
A story I was addicted to before building a Ruby and Rails environment using Ubuntu (20.04.1 LTS)
[Rails] I was addicted to the nginx settings when using Action Cable.
A story I was addicted to when testing the API using MockMVC
I was a little addicted to running old Ruby environment and old Rails
I want to use a little icon in Rails
I want to define a function in Rails Console
SpringSecurity I was addicted to trying to log in with a hashed password (solved)
I was addicted to starting sbt
What I was addicted to while using rspec on rails
I was addicted to setting default_url_options with Rails devise introduction
I was addicted to using Java's Stream API in Scala
A story I was addicted to when getting a key that was automatically tried on MyBatis
The story I was addicted to when setting up STS
A story I was addicted to with implicit type conversion of ActiveRecord during unit testing
A note when I was addicted to converting Ubuntu on WSL1 to WSL2
What I was addicted to when implementing google authentication with rails
A story addicted to JDBC Template placeholders
How to insert a video in Rails
I was addicted to rewriting to @SpringApplicationConfiguration-> @SpringBootTest
I was addicted to the roll method
Steps to set a favicon in Rails
I was addicted to the Spring-Batch test
[Rails] I want to send data of different models in a form
I was addicted to a simple test of Jedis (Java-> Redis library)
[CircleCI] I was addicted to the automatic test of CircleCI (rails + mysql) [Memo]
I was a little addicted to ssh connection from mac to linux (ubuntu)
I was addicted to unit testing with the buffer operator in RxJava
Convert to a tag to URL string in Rails
I was addicted to using RXTX on Sierra
A story addicted to EntityNotFoundException of getOne of JpaRepository
I tried to organize the session in Rails
A story that I was addicted to twice with the automatic startup setting of Tomcat 8 on CentOS 8
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
I was addicted to installing Ruby/Tk on MacOS
How to implement a like feature in Rails
How to easily create a pull-down in Rails
How to make a follow function in Rails
[Rails / JavaScript / Ajax] I tried to create a like function in two ways.
I was a little addicted to the S3 Checksum comparison, so I made a note.
How to implement a like feature in Ajax in Rails
I was addicted to the record of the associated model
Throw raw SQL to a read replica in Rails
I tried to create a Clova skill in Java
I tried to make a login function in Java
[How to insert a video in haml with Rails]
How to write a date comparison search in Rails
[Rails 6] How to set a background image in Rails [CSS]
I want to click a GoogleMap pin in RSpec
[Rails] How to load JavaScript in a specific view
A memo that I was addicted to when making batch processing with Spring Boot
[Rails] I want to display the link destination of link_to in a separate tab
The part I was addicted to in "Introduction to Ajax in Java Web Applications" of NetBeans
A memorandum because I was addicted to the setting of the Android project of IntelliJ IDEA
What I was addicted to when trying to properly openAPI/Swagger documentation with Rails + Grape + Grape Swagger
I tried to express the phone number (landline / mobile phone) with a regular expression in Rails and write validation and test
The story when the test folder was not created in Rails
What I was addicted to when introducing the JNI library
I want to find a relative path in a situation using Path
What I fixed when updating to Spring Boot 1.5.12 ・ What I was addicted to