Forum: Ruby on Rails chapter 11, second edition.. little help please.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Michael S. (Guest)
on 2007-01-16 23:47
(Received via mailing list)
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:         </p>
8:         <p>
9:         <label for="user_password">Password:</label>
10:         <%= form.password_field :password, :size => 40%>
11:         </p>
12:         <label for="user_password_confirmation">Confirm:</label>
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

<fieldset>
    <legend>Create User</legend>
    <% 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 %>
</fieldset>


--
-mike
Dave T. (Guest)
on 2007-01-17 06:02
(Received via mailing list)
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
Michael S. (Guest)
on 2007-01-18 02:24
(Received via mailing list)
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> 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
This topic is locked and can not be replied to.