I am trying to use a model altered to support bcrypt password, like so
require 'bcrypt'
class User < ActiveRecord::Base
# users.password_hash in the database is a :string
include BCrypt
def password
@password ||= Password.new(password_hash)
end
def password=(new_password)
@password = Password.create(new_password)
self.password_hash = @password
end
end
I added this in AFTER the scaffolding, hoping that the password and password= was enough to keep it going, using this, However it is not working, the form_for() etc wont work and I get the error:
ActionView::Template::Error (invalid hash):
2012-06-07T12:45:20+00:00 app[web.1]: 17: </div>
2012-06-07T12:45:20+00:00 app[web.1]: 18: <div class="field">
2012-06-07T12:45:20+00:00 app[web.1]: 19: <%= f.label :password %><br />
2012-06-07T12:45:20+00:00 app[web.1]: 20: <%= f.text_field :password %>
2012-06-07T12:45:20+00:00 app[web.1]: 21: </div>
2012-06-07T12:45:20+00:00 app[web.1]: 22: <div class="field">
2012-06-07T12:45:20+00:00 app[web.1]: 23: <%= f.label :email %><br />
Does anyone know how I can make this work, I am new to Rails
This is On the New Function
Full backtrace as requested:
2012-06-12T18:23:57+00:00 app[web.1]: ActionView::Template::Error (invalid hash):
2012-06-12T18:23:57+00:00 app[web.1]: 19: <%= f.label :password %><br />
2012-06-12T18:23:57+00:00 app[web.1]: 18: <div class="field">
2012-06-12T18:23:57+00:00 app[web.1]: 17: </div>
2012-06-12T18:23:57+00:00 app[web.1]: 20: <%= f.text_field :password %>
2012-06-12T18:23:57+00:00 app[web.1]: 22: <div class="field">
2012-06-12T18:23:57+00:00 app[web.1]: 23: <%= f.label :email %><br />
2012-06-12T18:23:57+00:00 app[web.1]: app/models/user.rb:19:in `new'
2012-06-12T18:23:57+00:00 app[web.1]: 21: </div>
2012-06-12T18:23:57+00:00 app[web.1]: app/models/user.rb:19:in `password'
2012-06-12T18:23:57+00:00 app[web.1]: app/views/users/_form.html.erb:20:in `block in _app_views_users__form_html_erb__2064609863987267967_31546180'
2012-06-12T18:23:57+00:00 app[web.1]: app/views/users/_form.html.erb:1:in `_app_views_users__form_html_erb__2064609863987267967_31546180'
2012-06-12T18:23:57+00:00 app[web.1]: app/views/users/new.html.erb:3:in `_app_views_users_new_html_erb___1991359801167056023_31763940'
2012-06-12T18:23:57+00:00 app[web.1]: app/controllers/users_controller.rb:251:in `new'
The exception message “invalid hash” indicates that your view is working just fine, but
User#passwordfails for whatever reason. Perhaps user doesn’t have a password_hash defined (ie, it is nil, “”, or some other invalid value), causingPassword.newto error out?Looking at your backtrace seems to reinforce this:
The error occurs in user.rb when calling
newinside yourpasswordmethod. Thus I am guessing line #19 isSo it looks like
Password.newdoes not like the value ofpassword_hashfor some reason. Exactly why, I can only guess at.Password?password_hash?