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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
On Jan 16, 2007, at 2:06 PM, Michael Steinfeld wrote:> 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):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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Michael Steinfeld
2007-Jan-17 23:25 UTC
Re: chapter 11, second edition.. little help please.
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.passwordNoMethodError: 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 Thomas <dave-kbbdpT5sCmpWk0Htik3J/w@public.gmane.org> wrote:> > > On Jan 16, 2007, at 2:06 PM, Michael Steinfeld wrote: > > 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): > > > > 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 > > > >-- -mike --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---