I am trying to set password with bcrypt-ruby and mongoid but whatever I
do I get "Password digest can't be blank".
# Model
class User
include Mongoid::Document
include Mongoid::Timestamps
include ActiveModel::SecurePassword
field :first_name, type: String, default: ''
field :last_name, type: String, default: ''
field :password_digest, type: String
has_secure_password
attr_accessible :name, :password, :password_confirmation
end
# Form part
<tr><td class="dc-form-label">
<label for="record_password">Password:</label></td>
<td class="dc-form-field">
<input id="record_password" type="password" size="20"
name="record[password]"></td>
</tr>
<tr>
<td class="dc-form-label">
<label for="record_password_confirmation">Password
confirmation:</label></td>
<td class="dc-form-field">
<input id="record_password_confirmation" type="password" size="20"
name="record[password_confirmation]"></td>
</tr>
-----------------------------
I have tried a lot. Funny thing is that editing or adding user with
rails console works OK.
One possible problem is that I have my own form system and I always use
name "record" for table name (instead of user in the case).
I am trying to set password with bcrypt-ruby and mongoid but whatever I
do I get "Password digest can't be blank".
# Model
class User
include Mongoid::Document
include Mongoid::Timestamps
include ActiveModel::SecurePassword
field :first_name, type: String, default: ''
field :last_name, type: String, default: ''
field :password_digest, type: String
has_secure_password
attr_accessible :name, :password, :password_confirmation
end
# Form part
<tr><td class="dc-form-label">
<label for="record_password">Password:</label></td>
<td class="dc-form-field">
<input id="record_password" type="password" size="20"
name="record[password]"></td>
</tr>
<tr>
<td class="dc-form-label">
<label for="record_password_confirmation">Password
confirmation:</label></td>
<td class="dc-form-field">
<input id="record_password_confirmation" type="password" size="20"
name="record[password_confirmation]"></td>
</tr>
-----------------------------
I have tried a lot. Funny thing is that editing or adding user with
rails console works OK.
One possible problem is that I have my own form system and I always use
name "record" for table name (instead of user in the case).
I don't understand, do you mean the the code you have shown is not
your actual code? It is always better to copy/paste your code in case
of typos.
Have you remembered to add the password_digest field to the table?
Everything looks fine with your model/controller. The problem is
probably coming from your view. Could you please include the full
snippet form in the view please?
ActiveModel::SecurePassword implements password= method, which is used
to set password_digest field.
so: calling record.password= sets password_diggest field.
But as I have written I am using my own form system, which uses single
controller for editing all models. I had something like this in the
controller:
fields.each do |k,v|
@record[v['name']] = params['record'][v['name']]
end
which leads to when v is 'password'
@record['password'] = params['record']['password']
which works perfectly OK with mongoid and adds password attribute to
document.
And of course doesn't call @record.password= method at all.