Reputation: 5836
I'm testing for attribute response in my model:
it { should respond_to(:password) }
it { should respond_to(:password_confirmation) }
These attributes aren't part of the database but simply declared in my model as attr_accessible
. When I don't declare them and run my tests I get:
ActiveModel::MassAssignmentSecurity::Error:
Can't mass-assign protected attributes: password, password_confirmation
But after I declare them I get:
ActiveRecord::UnknownAttributeError:
unknown attribute: password
Any idea why this happens?
Upvotes: 2
Views: 786
Reputation: 328
@8vius because you are following the tutorial, but not closely enough. You need to add the line:
has_secure_password
below attr_accessible :email, :name, :role, :password, :password_confirmation
this allows you to save the plain text password and password_confirmation into memory so that you can compare the strings and enforce equality before encrypting and saving into the DB. You don't want to persist the password in plain text on the db.
Upvotes: 3
Reputation: 115521
attr_accessible
tells Rails you allow so called mass assignement on attributes.
But attributes must exist in db or you should create getter/setter, the easiest means is:
attr_accessor :password_confirmation, :password
Anyway, sounds weird you don't store the password.
Upvotes: 2