0

I have a user model

class User < ApplicationRecord
  include ApplicationConstants

  enum role: { admin: 0, waiter: 1, chef:2 }

  has_secure_password

  validates :name, :role, presence: true, on: :create
  validates :email, uniqueness: true, format: EMAIL_REGEX
  validates :password, length: { minimum: 8 }, allow_blank: true
end

The controller definition is:

before_action :set_user, only: [:show, :update, :destroy]

def update
  if @user.update(user_params)
    render json: @user
  else
    render json: @user.errors, status: :unprocessable_entity
  end
end

def set_user
  @user = User.find(params[:id])
end

def user_params
  params.permit(:name, :role, :email, :password, :password_confirmation)
end

The problem is the following test case fails

test "should update user" do
  put user_url(@user), params: { name: @user.name }
  assert_response 200
end

The error is: {"password":["can't be blank"]}

I tried other answers like the one listed here, but it didn't work

Hariraj
  • 65
  • 8
  • 2
    One of the validations for `has_secure_password` is the password must be present on creation. See https://api.rubyonrails.org/classes/ActiveModel/SecurePassword/ClassMethods.html. -> "This mechanism requires you to have a XXX_digest attribute. Where XXX is the attribute name of your desired password" – Matt Jun 10 '20 at 19:33

1 Answers1

0

As Matt pointed out, it was because of the digest attribute being nil. I added it to the fixtures and now it works.

Hariraj
  • 65
  • 8