[RUBY] [Rails] A story that continued to incorrectly verify the reason why the update action did not pass (update)

1. What I wanted to do

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.

2. Execution environment

Ruby 2.6.5 Rails 6.0.3

3. Preparations made as a premise

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.

スクリーンショット 2020-09-02 12.00.22.png

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?

4. Conclusion

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.

5. Mistakes in the quagmire

スクリーンショット 2020-09-02 14.49.05.png

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

[Rails] A story that continued to incorrectly verify the reason why the update action did not pass (update)
The story of introducing Gradle as a retrofit to an existing system that did not manage packages
The story that did not disappear when I tried to delete mysql on ubuntu
A story that did not work when trying to handle events in Notification Center
The story that the forced update could not be implemented
The story that led to solving the error because postgres did not start with docker-compose up
The story that the DB connection and other timeouts did not time out according to the set value
[Gradle] The story that the class file did not exist in the jar file
The story that the build error did not stop when using Eclipse 2020
Correspond to "error that basic authentication does not pass" in the test code "The story that could not be done"
How to interact with a server that does not crash the app
A story that was embarrassing to give anison file to the production environment
[Rails] How to pass validation such as password when executing update action
A story about sending a pull request to MinGW to update the libgr version
After verifying the Monty Hall problem with Ruby, a story that I could understand well and did not understand well
A story that people who did iOS solidly may be addicted to the implementation of Listener when moving to Android