[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
ActiveRecord :: NotNullViolation in Devise error
[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
[Rails5] Rspec -validation-
Create authentication function in Rails application using devise
The road to Japaneseizing Rails devise error messages
Error in bundle install when running rails new
[Rails] Solution when migration error occurs in acts-as-taggable-on
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)
Catch Rails Routing Error
Model association in Rails
[Rails] devise helper method
Adding columns in Rails
Introduce devise in Rails to implement user management functionality
Japaneseization of Rails error messages [devise / Form objects, etc.]
Disable turbolinks in Rails
Migration error after Activerecord association in Rails5 + Docker environment
Mac Rails Install Error
Handle devise with Rails
[Rails] Cancel / change the default password validation of devise
^, $ 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
[Rails] Workaround for classes automatically generated by devise error messages
[Rails] I implemented the validation error message by asynchronous communication!
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
Get UserAgent in [Rails] controller
Spring validation and error code
[Rails] Introduction of devise Basics
Avoid Yubaba's error in Java
Declarative transaction in Rails #ginzarb
What is Rails gem devise?
NameError in Income # index error
About validation methods in JUnit
[Rails] Japanese longest surname (validation)
rails error Library not loaded
[Rails] gem devise installation flow
Enable strong parameters in devise