Topic: Where is an error: RESTful authenticatin?

Why I'm getting this all the time?

ArgumentError in UsersController#create

wrong number of arguments (0 for 1)

Application Trace | Framework Trace | Full Trace
vendor/rails/activerecord/lib/active_record/callbacks.rb:337:in `encrypt_password'
vendor/rails/activerecord/lib/active_record/callbacks.rb:337:in `send'
vendor/rails/activerecord/lib/active_record/callbacks.rb:337:in `callback'
vendor/rails/activerecord/lib/active_record/callbacks.rb:334:in `each'
vendor/rails/activerecord/lib/active_record/callbacks.rb:334:in `callback'
vendor/rails/activerecord/lib/active_record/callbacks.rb:242:in `create_or_update'
vendor/rails/activerecord/lib/active_record/base.rb:1693:in `save_without_validation'
vendor/rails/activerecord/lib/active_record/validations.rb:848:in `save_without_transactions'
vendor/rails/activerecord/lib/active_record/transactions.rb:105:in `save'
vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:59:in `transaction'
vendor/rails/activerecord/lib/active_record/query_cache.rb:66:in `send'
vendor/rails/activerecord/lib/active_record/query_cache.rb:66:in `method_missing'
vendor/rails/activerecord/lib/active_record/transactions.rb:77:in `transaction'
vendor/rails/activerecord/lib/active_record/transactions.rb:97:in `transaction'
vendor/rails/activerecord/lib/active_record/transactions.rb:105:in `save'
vendor/rails/activerecord/lib/active_record/transactions.rb:117:in `rollback_active_record_state!'
vendor/rails/activerecord/lib/active_record/transactions.rb:105:in `save'
app/controllers/users_controller.rb:46:in `create'
app/controllers/users_controller.rb:45:in `create'


My user.rb model is:

require 'digest/sha1'

class User < ActiveRecord::Base
  attr_accessor :password
  attr_protected :crypted_password, :salt, :created_at, :updated_at

  validates_presence_of     :password_confirmation,      :if => :password_required?
  validates_length_of       :password, :within => 5..40, :if => :password_required?
  validates_confirmation_of :password,                   :if => :password_required?
  validates_length_of       :username, :within => 3..40
  validates_uniqueness_of   :username
  validates_format_of       :username, :with => /^([a-z0-9-]+\.){0,2}[a-z0-9-]+$/i, :on => :create,
                            :message => 'can only contain letters, hyphen, digits, and two dots.',
                            :if => Proc.new {|user| user.username && !user.username.empty? }
  validates_exclusion_of    :username, :in => %w( www ),
                            :message => "'www' is not a valid username."

  before_save :encrypt_password

  # Authenticates a user by their username and unencrypted password. Returns the User or nil.
  # ====Parameters
  # username:: The User's username.
  # password:: The user's unencrypted password.
  def self.authenticate(username, password)
    u = self.find_by_username(username)
    u && u.authenticated?(password) ? u : nil
  end

  # Returns true if _password_ matches the User's password.
  # ====Parameters
  # password:: The User's unencrypted password.
  def authenticated?(password)
    crypted_password == encrypt_password(password)
  end

  protected
    def encrypt(data, salt)
      Digest::SHA1.hexdigest("--#{salt}--preved--#{data}--")
    end

    def encrypt_password(password)
      return if password.blank?
      self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{username}--") if new_record?
      self.crypted_password = encrypt(password, salt)
    end

    def password_required?
      (crypted_password.blank? || !password.blank?)
    end
end


Where is an error?

Re: Where is an error: RESTful authenticatin?

before_save :encrypt_password
...

def encrypt_password(password)
...
end


You're calling encrypt_password without a password argument. In this case, you can remove password from the params list:

def encrypt_password

Vincent Woo Ruby on Rails Blog