LoginSignup
15
14

More than 5 years have passed since last update.

ユーザの情報をパスワードなく更新したい

Posted at

問題

  • サインインしたユーザの情報を更新したいができなかった。

  • Rails.logger.info(@user.errors.messages.inspect) でログを見てみると、どうもパスワードがないため更新されない感じだった。

解決策

モデルで allow_blankを適応させた

model.rb

validates :password, presence: true, length: {minimum: 5, maximum: 120}

だった場合、

model.rb

validates :password, presence: true, length: {minimum: 5, maximum: 120}, on: :create
validates :password, length: {minimum: 5, maximum: 120}, on: :update, allow_blank: true

のように、updateが呼ばれるときだけはallow_blankで行う。

※ validates が重複してるので、うまくDRYでいける方法があれば教えて欲しいです…

参考

Devise 3 (rails 4) can't update user without password - Stack Overflow

15
14
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
14