chapter 11, second edition.. little help please.

I have followed the examples in Chapter 11 page 147-155 and can’t get the ’ Create User ’ to work. I’m new to ruby so maybe there is a syntax error or something. Here is what I have and the error. Please let me know if you see any mistakes.

Here is the error.

NoMethodError in Login#add_user

Showing app/views/login/add_user.rhtml where line #10 raised:

undefined method `password’ for #User:0x2aa0abc
Extracted source (around line #10):

7:


8:


9: Password:
10: <%= form.password_field :password, :size => 40%>
11:

12: Confirm:
13: <%= form.password_field :password_confirmation, :size => 40%>

… Than the code …

LOGIN CONTROLLER …

class LoginController < ApplicationController

layout “admin”

def add_user
@user = User.new(params[:user])
if request.post? and @user.save
flash.now[:notice] = “User #{@
user.name} created successfully!”
@user = User.new
end
end

USER.RB

require “digest/sha2”

class User < ActiveRecord::Base
validate_presence_of :name
validates_uniqueness_of :name

attr_accessor :password_confirmation
validates_confirmation_of :password

def validate
errors.add_to_base(“Missing Something”) if hashed_password.blank?

end

def self.authenticate(name, password)
user = self.find_by_name(name)
if user
expected_password = encrypted_password(password, user.salt)
if user.hashed_password != expected_password

     user = nil
   end
 end
 user

end

def password
@password
end

def password=(pwd)
@password = pwd
create_new_salt
self.hashed_password
= User.encrypted_password(self.password, self.salt)
end

private

def self.encrypted_password(password, salt)
string_to_hash = password + “bobblehead” + salt #bobblehead to random the pass for security

Digest::SHA256.hexdigest(string_to_hash)

def create_new_salt
self.salt = self.object_id.to_s + rand.to_s
end
end
end

ADD_USER.RHTML

Create User
<% form_for :user do |form| %>
    <p>
    <label for="user_name">Name:</label>
    <%= form.text_field  :name, :size => 40 %>
    </p>
    <p>
    <label for="user_password">Password:</label>
    <%= form.password_field :password, :size => 40%>
    </p>
    <label for="user_password_confirmation">Confirm:</label>

    <%= form.password_field :password_confirmation, :size => 40%>
    </p>
    <p>
    <%= submit_tag "Add User", :class => "submit" %></p>

<% end %>

Here’s what I’d do:

Bring up script/console

Create a new user: u = User.new

See if you can access the password attribute: p u.password

It should return nil

If it works, then I’d try restarting your application: maybe the change wasn’t reloaded.

If it doesn;t work, then maybe you haven’t saved the change, or you’re working in a different source tree?

Dave

well here is the error that baffled me when using script/console

>> mike = User.create(:name => "mike")
=> #<User:0x28cbdf4 @new_record=false, @errors=#<ActiveRecord::Errors:0x28cad50 @base=#<User:0x28cbdf4 ...>, @errors={}>, @attributes={"salt"=>nil, "name"=>"mike", "hashed_password"=>nil, "id"=>20}>

>> mike.hashed_password = "foo"
=> "foo"
>> mike.salt
=> nil
>> mike.password

NoMethodError: undefined method `password' for #<User:0x28cbdf4>
        from ./script/../config/../config/../vendor/rails/activerecord/lib/active_record/base.rb:1847:in `method_missing'

        from (irb):25
>> mike
=> #<User:0x28cbdf4 @new_record=false, @errors=#<ActiveRecord::Errors:0x28cad50 @base=#<User:0x28cbdf4 ...>, @errors={}>, @attributes={"salt"=>nil, "name"=>"mike", "hashed_password"=>"foo", "id"=>20}>

>> mike.hashed_password
=> "foo"

I did restart the server, that did seem to fix it, then I ran "rake rails:update" oddly enough it worked after that

I had already downloaded the zip file and ran this after I created the application. Is it coincedence after restarting the server?

thanks for your help.

mike