While creating the flea market apps, I couldn't figure out why the data wasn't saved even though I was able to get the key. When I asked the mentor, he told me how to find the cause, so make a note.
A method for saving data.
The save method returns true if the save is successful and false if the save fails (validation fails).
The save! method returns true if the save is successful, but an exception is thrown if the save fails (validation fails). Therefore, it is necessary to perform the processing in the rescue clause when it cannot be saved.
■ In my case, when I used binding.pry
, I was able to get the key, so I used@user.save!
To find the cause.
↓ Description when it could not be saved
[8] pry(#<Users::RegistrationsController>)> @user.save!
(0.2ms) BEGIN
↳ (pry):12
User Exists (13.4ms) SELECT 1 AS one FROM `users` WHERE `users`.`email` = BINARY '' LIMIT 1
↳ (pry):12
(1.1ms) ROLLBACK
↳ (pry):12
ActiveRecord::RecordInvalid: Validation failed: Email can't be blank, Email is invalid, Password can't be blank, Nickname can't be blank, Encrypted password can't be blank, Encrypted password is too short (minimum is 7 characters), Encrypted password is invalid, Familyname kanji can't be blank, Familyname kanji is invalid, Firstname kanji can't be blank, Firstname kanji is invalid, Familyname kana can't be blank, Familyname kana is invalid, Firstname kana can't be blank, Firstname kana is invalid, Birthday can't be blank
from /Users/httr_htm/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.4.3/lib/active_record/validations.rb:80:in `raise_validation_error'
The cause is written after ʻActiveRecord :: RecordInvalid`.
As a result, the description of the regular expression was the cause. I had set the email in the format of ʻ[email protected], but I was stuck in validation because I entered ʻaa @ aa
. If I had set the error display, I might have been able to solve it sooner.
I was introduced to a site that checks regular expressions called Rubular. I want to utilize it in the future.
Active engineers explain how to use Rails' save! Method [for beginners]
Recommended Posts