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.
** Be careful when you validate your User model yourself. ** **
I will do some preliminary research and implement it in the future. ..
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
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).
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. ..
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.
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!
https://qiita.com/hirokihello/items/862284c60429be5e01cd https://github.com/heartcombo/devise/wiki/Customize-minimum-password-length
Recommended Posts