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
Should I Check in '.Ruby-Gemset' And/Or '.Ruby-Version'
Differencebetween #Encode and #Force_Encoding in Ruby
Normalizing Line Endings in Ruby
How to Escape Strings for Terminal in Ruby
Using Yield Inside Define_Method in Ruby
Carrierwave - Resizing Images to Fixed Width
How to Render a Plain HTML File with Sinatra
How to Convert a JSON String to an Object
Share Models Between 2 Rails API's (Separate Applications)
How to Extract the Sign of an Integer in Ruby
Getting the Full Rspec Test Name from Within a Before(:Each) Block
Ruby Regex - Gsub Only Captured Group
How to Get All Sundays Between Two Dates in Ruby
Rails 3.1 Actioncontroller::Routingerror (No Route Matches [Get] "/Assets/Rails.Png"):
In Ruby What Does the "Receiver" Refer To