mreysset
mreysset

Reputation: 89

Rails 3 - wrong number of arguments (0 for 1)

I'm trying to build an authentication system for my application

to register the user fills in a form which then submitted calls user#signup

def signup  
    @user = User.new(params[:user])
    @user.password(params[:user][:password])
    @user.save 
end`

My user model contains

def password(pass)
    @password=pass
    self.salt = User.random_string(10)
    self.hashed_password = User.encrypt(@password, self.salt)
end

When trying to debug, I checked what is contained in my @user hash:

Stack trace

Started POST "/users/signup" for 127.0.0.1 at Fri Oct 05 14:23:49 +1000 2012
Processing by UsersController#signup as HTML
  Parameters: {"user"=>{"last_name"=>"last", "first_name"=>"first", "login"=>"myusername", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "email"=>"[email protected]", "dob(1i)"=>"1980", "dob(2i)"=>"4", "dob(3i)"=>"2"}, "authenticity_token"=>"R8/BNPox9F9rkUXZQ84xjnsplRjqLJYe35EtGjKEAWk=", "utf8"=>"✓", "commit"=>"Create User"}
   (0.1ms)  begin transaction
   (0.0ms)  rollback transaction
Completed 500 Internal Server Error in 10ms

ArgumentError (wrong number of arguments (0 for 1)):
  app/controllers/users_controller.rb:17:in `signup'
  app/controllers/users_controller.rb:16:in `signup'


  Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.2.8/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.1ms)
  Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.2.8/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.1ms)
  Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.2.8/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (7.8ms)

Any idea?

Upvotes: 2

Views: 2617

Answers (1)

willglynn
willglynn

Reputation: 11520

I'm guessing you have a column called password and/or you have a validation referencing :password.

In that case, the problem is that you've overridden password -- the implicit zero-parameter getter -- with password(pass), a one-parameter setter. You should instead move your setter to password=, and make a getter that always returns nil:

def password
  nil
end

def password=(pass)
  @password = pass
  self.salt = User.random_string(10)
  self.hashed_password = User.encrypt(@password, self.salt)
end

As a bonus, this means you can eliminate the explicit password-setting call, since User.new(:password => 'xyz') will call password= automatically.

Upvotes: 1

Related Questions