Cant Use Has_Secure_Password, Password_Digest Error

Rails: Password can't be blank with has_secure_password

The actual reason for the error is you mistyped password as pasword in your user_params. Fixing the typo should solve your problem.

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

Rails API returning password can't be blank with has_secure_password

You are not permitting the password in user_params. May be something like following can work

def user_params
params.require(:user).permit(:email, :password, :is_admin, :is_agent)
end

More on password digests here https://medium.com/@tpstar/password-digest-column-in-user-migration-table-871ff9120a5

Also, the JSON request needs to be wrapped in a user object:

{
"user":
{
"email": "user@example.com",
"password": "password",
"is_admin": true,
"is_agent": true
}
}

Password digest can't be blank

Your log is missing both the password and password_confirmation fields being set. It should look more like this

{"utf8"=>"✓", "authenticity_token"=>"dsdfhjdskhfsdfhjsdfhjsdfhjdsfhjsdfE=", "user"=>{"email"=>"email@provider.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Register"}

cant use has_secure_password, password_digest error

You may have forgotten to make sure your migration backing the user model has a column for password_digest. Make sure the column exists and that it's a string. If it doesn't, create a migration to add the column.

Validation failed: Password digest can't be blank

In all the failing tests, you use @attr = { :name => "Example User", :email => "user@example.com" } to create a new user. But to create a new user, you MUST set password and password_confirmation. Add these attributes to the @attr hash. The password_digest field should then be set automatically and it should be possible to save the user.

Ruby - ActiveModel::SecurePassword not working

Since you need to have password_digested filled by ActiveModel::SecurePassword you have to call User#password= setter method. But it's not happening when you set your password using @password = password in your initializer. To fix it you have set it using self.password = password:

def initialize(name:, email:, password:) 
@name, @email = name, email
self.password = password
end

Also you need to remove :password from attr_accessor call because SecurePassword provides it.



Related Topics



Leave a reply



Submit