ArgumentError

hi, i’m very new to RoR,

i’m following an example on the book (Agile Web D. with Rails
2nd ed.)

i met an error that goes like this (even though i followed the book word
for word)

ArgumentError in Login#add_user
Showing app/views/login/add_user.rhtml where line #16 raised:

wrong number of arguments (0 for 1)

Extracted source (around line #16):

13:
14:


15: Password:
16: <%= form.password_field :password, :size => 40 %>
17:


18:
19:

here are my .rb and .rhtml

add_user.rhtml:

<%= error_messages_for ‘user’ %>

Enter User Details
<% 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>

  <p>
    <label for="user_password_confirmation">Confirm:</label>
    <%= form.password_field :password_confirmation, :size => 40 %>
  </p>

  <%= submit_tag "Add User", :class => "submit" %>

<% end %>

login_controller.rb:

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”
@user = User.new
end
end

def login
end

def logout
end

def index
end

def delete_user
end

def list_user
end
end


user.rb:

require ‘digest/sha1’

class User < ActiveRecord::Base

validates_presence_of :name
validates_uniqueness_of :name

attr_accessor :password_confirmation
validates_confirmation_of :password

def validate
errors.add_to_base(“Missing password!!!” ) 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 + "wibble" + salt
  Digest::SHA1.hexdigest(string_to_hash)
end

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

end


Thanks alot in advance, i’m really lost here!

On 25 Oct 2007, at 02:33, Jojo M. wrote:

i met an error that goes like this (even though i followed the book
word
for word)
but not symbol for symbol :_

ArgumentError in Login#add_user
Showing app/views/login/add_user.rhtml where line #16 raised:

wrong number of arguments (0 for 1)

Extracted source (around line #16):
16: <%= form.password_field :password, :size => 40 %>

def password
@password
end

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

So line 16 is trying to read the password attribute, ie it tries to
call the password method. You’ve defined the password method as
taking one argument, so it all falls over.
You probably meant
def password=(pwd)
@password = pwd

end

Fred

On 10/25/07, Frederick C. [email protected] wrote:

end

So line 16 is trying to read the password attribute, ie it tries to
call the password method. You’ve defined the password method as
taking one argument, so it all falls over.

Just to beat the h3ll out of the obvious. He RE-defined the password
method.

The fact that Ruby considers only the method name and not the
parameters is something which trips up folks who have used languages
like C++ and Java and are expecting to be able to overload methods.


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/