🦔

【rails7】でのエラー対応

2023/08/25に公開

ActiveRecord::RecordInvalid: Validation failed: Password can't be blank, Password is too shortが発生した時の対処法

環境

・Rails: 7.0
・ruby: 3.2
・devise_token_auth: 1.2
・devise: 4.9
・railsはAPIモードで利用

発生状況

以下ファイルで、 validatesを追記しました。

app/models/user.rb
validates :password, presence: true, length: { minimum: 2 }

エラー内容

postmanでログインしようとしてところ、下記のエラーが発生しました。

"status": 422,
"error": "Unprocessable Entity",
"exception": "#<ActiveRecord::RecordInvalid: Validation failed: Password can't be blank, Password is too short (minimum is 2 characters)>"

対処方法

deviceの公式ドキュメントでファイル内を変更すれば解決します。

app/models/user.rb
devise :database_authenticatable, :validatable, password_length: 2..128

参考になれば嬉しいです。

Discussion