Rails: password can't be blank, BCrypt

216 Views Asked by At

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

1

There are 1 best solutions below

0
Hariraj On

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