# Chapter 11, second edition.. little help please

**URL:** <https://www.ruby-forum.com/t/chapter-11-second-edition-little-help-please/82031>\
**Category:** Rails\
**Created:** [January 16, 2007, 10:47pm UTC](https://www.ruby-forum.com/t/chapter-11-second-edition-little-help-please/82031 "2007-01-16T22:47:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [January 16, 2007, 10:47pm UTC](https://www.ruby-forum.com/t/chapter-11-second-edition-little-help-please/82031/1 "2007-01-16T22:47:53Z")

</div>

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| %\> 

Name: \<%= form.text\_field :name, :size =\> 40 %\>

Password: \<%= form.password\_field :password, :size =\> 40%\>

 Confirm: \<%= form.password\_field :password\_confirmation, :size =\> 40%\> 

\<%= submit\_tag "Add User", :class =\> "submit" %\>

 \<% end %\>

–  
-mike

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [January 17, 2007, 5:02am UTC](https://www.ruby-forum.com/t/chapter-11-second-edition-little-help-please/82031/2 "2007-01-17T05:02:54Z")

</div>

On Jan 16, 2007, at 2:06 PM, Michael S. wrote:

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

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

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [January 18, 2007, 1:24am UTC](https://www.ruby-forum.com/t/chapter-11-second-edition-little-help-please/82031/3 "2007-01-18T01:24:07Z")

</div>

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

On 1/16/07, Dave T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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

–  
-mike
