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

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

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?

Colin

Colin Law 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

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

Attachments: http://www.ruby-forum.com/attachment/7745/myuser_form.html

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.