Troubles with bcrypt-ruby

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

Password: Password confirmation: ----------------------------- 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).

Help please.

by
TheR

On Sep 14, 2012, at 7:31 AM, Damjan R. wrote:

field :last_name, type: String, default: ‘’
<input id=“record_password” type=“password” size=“20”

----------------------------- 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).

This may be related:
http://reservedwords.herokuapp.com/words/records?q[word_or_notes_cont]=record

Walter

This may be related:
http://reservedwords.herokuapp.com/words/records?q[word_or_notes_cont]=record

Walter

Nope. I checked

by
TheR

On 14 September 2012 12:31, Damjan R. [email protected] wrote:

field :last_name, type: String, default: ‘’
<input id=“record_password” type=“password” size=“20”

----------------------------- 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?

Colin

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?

Attached is the generated html source code.

Thanks for any help
TheR

Colin L. wrote in post #1076008:

Have you remembered to add the password_digest field to the table?

Colin

Have you even tried to look at the code supplied ;-(

Code represents a simplified model that I run test on and it also does
not work. I don’t think that adding few pages of code would be of any
help.

by
TheR

It turned out the problem was in controller code.

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.


Solution is to use send(“password=”, value) method of @record.

@record.send( “#{v[‘name’]}=”, params[‘record’][v[‘name’]] )

Hope this will save somebodies hours in future.

by
TheR