ArgumentError:参数个数错误(2个为1)

时间:2012-12-29 16:58:52

标签: ruby-on-rails-3.2

我是ROR的新手并完成了Lynda.com教程。我已完成有关身份验证的部分,当我尝试在Rails控制台中保存对用户配置文件的更改时,我收到wrong number of arguments错误。

这是我从控制台获得的结果:

>Loading development environment (Rails 3.2.9)
>> user = AdminUser.find(1)
>  AdminUser Load (0.4ms)  SELECT `admin_users`.* FROM `admin_users` WHERE                 `admin_users`.`id` = 1 LIMIT 1
=> #<AdminUser id: 1, first_name: "Stuart", last_name: "Culpepper", email:     "stuart.culpepper@gmail.com", hashed_password: "962b8cf45f025b7e1d155529f8c4bf1c02782164",     created_at: "2012-12-20 22:12:43", updated_at: "2012-12-28 20:04:42", username: "stuartculpepper", salt: "f3e2b128c35f8ec163caed28c4addc9d8de2d186">
>> user.password = 'secret'
=> "secret"
>> user.save
   (0.1ms)  BEGIN
  AdminUser Exists (0.4ms)  SELECT 1 AS one FROM `admin_users` WHERE (`admin_users`.`username` = BINARY 'stuartculpepper' AND `admin_users`.`id` != 1) LIMIT 1
   (0.1ms)  ROLLBACK
ArgumentError: wrong number of arguments (2 for 1)
    from /Users/stuartculpepper/Sites/simple_cms/app/models/admin_user.rb:60:in `hash_with_salt'
    from /Users/stuartculpepper/Sites/simple_cms/app/models/admin_user.rb:60:in `create_hashed_password'

以下是我的UserAdmin模型中的代码。第60行注明的错误为self.hashed_password = AdminUser.hash_with_salt(password, salt)

require 'digest/sha1'

class AdminUser < ActiveRecord::Base

  # set_table_name("admin_users")

  has_and_belongs_to_many :pages
  has_many :section_edits
  has_many :sections, :through => :section_edits

  attr_accessor :password

  EMAIL_REGEX = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i

  #validates_presence_of :first_name
  #validates_length_of :first_name, :maximum => 25
  #validates_presence_of :last_name
  #validates_length_of :last_name, :maximum => 50
  #validates_presence_of :username
  #validates_length_of :username, :within => 8..25
  #validates_uniqueness_of :username
  #validates_presence_of :email
  #validates_length_of :email, :maximum => 100
  #validates_format_of :email, :with => EMAIL_REGEX
  #validates_confirmation_of :email

  #sexy validations
  validates :first_name, :presence => true, :length => {:maximum => 25}
  validates :last_name, :presence => true, :length => {:maximum => 50}
  validates :username, :length => {:within => 8..25}, :uniqueness => true
  validates :email, :presence => true, :length => {:maximum => 100}, :format => EMAIL_REGEX, :confirmation => true 


  # only on create, so other aspects of this user can be updated
  validates_length_of :password, :within => 8..25, :on => :create

  before_save :create_hashed_password
  after_save :clear_password

  scope :named, lambda {|first,last| where(:first_name => first, :last_name => last)}

  attr_accessible :first_name, :last_name, :email, :username 
  attr_protected :hashed_password, :salt

  def self.make_salt(username="")
    Digest::SHA1.hexdigest("Use #{username} with #{Time.now} to make salt")
  end

  def self.hash_with_salt(password="")
    Digest::SHA1.hexdigest("Put #{salt} on the #{password}")
  end

   private

   def create_hashed_password
     # Whenever :password has a value hashing is needed
     unless password.blank?
       # alwys use "self" when assigning values
       self.salt = AdminUser.make_salt(username) if salt.blank?
       self.hashed_password = AdminUser.hash_with_salt(password, salt)
     end
   end

   def clear_password
     # for security and b/c hashing is not needed
     self.password = nil
   end

end

欢迎任何想法,谢谢,如果错误在我面前,我道歉。我已经查看了stackoverflow上的所有类似问题,并且似乎无法理解我应该如何修复它,尽管我会继续尝试。 ; - )

1 个答案:

答案 0 :(得分:1)

您似乎忘了添加第二个参数salt

def self.hash_with_salt(password="")
                                  ^

这就是你打电话时出错的原因:

self.hashed_password = AdminUser.hash_with_salt(password, salt)