User registration (named owner) function has been implemented using gem devise. When I tried to edit the registered data without entering the password, an error occurred and I was worried about solving it.
Ruby 2.6.5 Rails 6.0.3
The following two preparations were required according to the specifications on the devise side. (1) Allow values for columns other than the default column during account_update (sanitizer) (2) Allow updates without a password.
The official here or [here](https://github.com/heartcombo/devise/wiki/How-To:-Allow-users-to-edit- You can do it by looking at their-account-without-providing-a-password) article. If you check Qiita, there should be many.
Although these were made, the update could not be saved properly for some reason as shown below, probably because of the devise specifications ...? To be worried in agony.
During the above error verification, I was worried as follows.
1.ArgumentError given 0 ~, so isn't the argument passed in the first place? (But params goes through properly ...) 2. If the error message is a password, isn't devise's passwordless update not working?
It was an error because the password validation was stuck.
owner.rb
class Owner < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :name, presence: true
validates :email, presence: true, uniqueness: true, format: { with: VALID_EMAIL_REGEX}
validates :password, presence: true, confirmation: true, length: { minimum: 7 }
has_many :shops
end
The validation of the above model was also applied at the time of update, so it is a form that an error occurred. If you think about it carefully, the error message set for validation is output, so it's natural, isn't it?
So
owner.rb
class Owner < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :name, presence: true
validates :email, presence: true, uniqueness: true, format: { with: VALID_EMAIL_REGEX}
validates :password, presence: true, confirmation: true, length: { minimum: 7 },on: :create
has_many :shops
end
It was solved by setting password validation only at the time of create like this.
Regarding the error message at the time of binding.pry earlier, I thought that the argument was not passed well here, but that recognition was wrong in the first place.
After the argument is passed properly in the update action of the controller, update is executed again on the debug screen, isn't it? So the update I said on this screen doesn't have any arguments, so I'm just getting an error saying that there are no arguments.
It was a mistake I made because I didn't understand this flow properly ... Reflection.
Recommended Posts