I am testing my user input validation in my application and I am getting two errors in regards to my password presence.
This is what I have written for my model.
class User < ActiveRecord::Base
include Slugifiable
extend Slugifiable::Find
has_secure_password
has_many :posts
validates :email, uniqueness: true, presence: true
validates :username, uniqueness: true, presence: true
validates :password, presence: true
end
Below is my migration table:
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :email
t.string :username
t.string :password
t.string :password_digest
end
end
end
Each time I run my application with no input it should give me three error messages: "Password can't be blank", "Email can't be blank", "Username can't be blank". Instead I get an extra "Password can't be blank" error. I am using a password_digest
variable which is a salted hash of the users password once the data persists in the database.
has_secure_password
comes with its own presence validation on the create
action. Therefore, validating the presence of password
is redundant and is causing you to get two "Password can't be blank" error messages.
Simply remove validates :password, presence: true
or add a condition to the validation for a specific controller action/other context...ie
validates :password, presence: true, on: :some_action