[Rails] Unexpected validation error in devise

Introduction

I was addicted to an unexpected error with devise, which is taken care of by user management, and melted a considerable amount of time, so I will leave it as a memorandum.

Environment / Gem (ver)

Conclusion

** Be careful when you validate your User model yourself. ** **

I will do some preliminary research and implement it in the future. ..

Premise

After bundle install devise, I validated the User model as follows: I wanted to limit the password to 7 characters or more.

You have successfully registered a new user.

(models/user.rb)
class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable

  has_many   :stylists
  has_many   :shops, through: :stylists

  (abridgement)

  validates :password, presence: true, length: { minimum: 7 }, format: { with: /(?=.*\d+.*)(?=.*[a-zA-Z]+.*)./ }
end 

phenomenon

An error occurs when trying to create an intermediate table (Stylist model) at the same time as creating a new shop (Shop model) after registering a user (User model).

Image from Gyazo

The parameters have values, and I can't figure out what is ** "illegal value" **. Also, in the situation of ** "I can't register (new / create) but can edit (edit / update)" **, the mystery deepened and I spent a lot of time. ..

hypothesis

Isn't it solved by allowing the foreign key nil?

(models/stylist.rb)
class Stylist < ApplicationRecord
#Before correction
  belongs_to :shop
  belongs_to :user
#Revised
  belongs_to :shop, optional: true
  belongs_to :user, optional: true
end

···It was bad.

solution

There is no doubt that the cause is validation, so if you check the behavior while canceling the validates described in the User model one by one,

(models/user.rb)
class User < ApplicationRecord

  (abridgement)

  validates :password, presence: true, length: { minimum: 7 }, format: { with: /(?=.*\d+.*)(?=.*[a-zA-Z]+.*)./ }
end 

↑ validates: If you delete the password, everything was solved! !!

And I rewrote the customization contents in the following places.

(config/initializers/devise.rb)
class User < ApplicationRecord

  (abridgement)

#Before correction
  config.password_length = 6..128
#Revised
  config.password_length = 7..128

Why throw an error when saving an intermediate table in the first place? It's still a mystery, but devise has a default validation, and I think it did a bad reaction with the validation I made. .. (Will study)

In addition, I referred to the following article and customized it, and it works fine!

Reference article

https://qiita.com/hirokihello/items/862284c60429be5e01cd https://github.com/heartcombo/devise/wiki/Customize-minimum-password-length

Recommended Posts

[Rails] Unexpected validation error in devise
[Rails] Customize devise validation
[Rails error] unexpected tIDENTIFIER
ActiveRecord :: NotNullViolation in Devise error
Error in rails db: migrate
[rails] error during devise installation
[Rails] Session timeout setting in devise
[rails] Login screen implementation in devise
[Rails] devise
[Rails] syntax error, unexpected tSTRING_END, expecting'''
[Rails] Function restrictions in devise (login / logout)
Error in implementation when implementing Spring validation
[rails] Set validation
[Rails] Introducing devise
Group_by in Rails
rails + devise + devise_token_auth
[Rails5] Rspec -validation-
Create authentication function in Rails application using devise
The road to Japaneseizing Rails devise error messages
Rails: Japanese localization of validation messages including devise
Error in bundle install when running rails new
[Rails] Solution when migration error occurs in acts-as-taggable-on
[Rails] Japanese localization of validation error message ~ ja.yml ~
500 Internal Server Error occurs in Rails production environment
[Rails] How to solve "Uglifier :: Error: Unexpected character'`'"
[Rails] Added in devise: username not added to database
[Rails] ActiveRecord :: HasManyThrough Order Error in Users # show
[Note] Rails error list
Migration error after Activerecord association in Rails5 + Docker environment (2)
One case of solving a migration error in Rails
[Ruby on Rails Tutorial] Error in the test in Chapter 3
[Rails 6] Validation by belongs_to
Model association in Rails
Adding columns in Rails
Introduce devise in Rails to implement user management functionality
Japaneseization of Rails error messages [devise / Form objects, etc.]
[Rails] About error resolution when installing devise and activeadmin
Disable turbolinks in Rails
Migration error after Activerecord association in Rails5 + Docker environment
CSRF measures in Rails
Mac Rails Install Error
Handle devise with Rails
[Rails] Cancel / change the default password validation of devise
rails heroku error log
rails error resolution summary
^, $ in Rails regular expression
Use images in Rails
Error handling in Graphql-ruby
[Rails] devise introduction method
Understand migration in rails
[rails] About devise defaults
Split routes.rb in Rails6
Implement markdown in Rails
exited with code 1 error resolution with docker-compose up in rails environment
[Rails] I implemented the validation error message by asynchronous communication!
Rails: You need to supply at least one validation error
A story I was addicted to in Rails validation settings
Super easy in 2 steps! How to install devise! !! (rails 5 version)
[Rails] Where to be careful in the description of validation
[rails] Problems that cannot be registered / logged in with devise
Implement user registration function and corporate registration function separately in Rails devise