Ruby on rails 3.2 ArgumentError:参数数目错误(2代表1)

Ruby on rails 3.2 ArgumentError:参数数目错误(2代表1),ruby-on-rails-3.2,Ruby On Rails 3.2,我是ROR的新手,正在完成Lynda.com教程。我已经完成了一个关于身份验证的部分,当我试图在Rails控制台中保存对用户配置文件的更改时,得到了一个错误的参数数错误 这是我从控制台得到的结果: >Loading development environment (Rails 3.2.9) >> user = AdminUser.find(1) > AdminUser Load (0.4ms) SELECT `admin_users`.* FROM `admin_us

我是ROR的新手,正在完成Lynda.com教程。我已经完成了一个关于身份验证的部分,当我试图在Rails控制台中保存对用户配置文件的更改时,得到了一个
错误的参数数
错误

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

>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'

任何想法都是欢迎的,谢谢,如果错误就在我面前,我道歉。我查看了stackoverflow上所有类似的问题,似乎不明白应该如何解决它,尽管我会继续尝试。;-)

看起来您忘记添加第二个参数了
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
def self.hash_with_salt(password="")
                                  ^
这就是为什么您在调用时出现错误:

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

您似乎忘记添加第二个参数
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
def self.hash_with_salt(password="")
                                  ^
这就是为什么您在调用时出现错误:

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

非常感谢@Knownasilya!一旦你指出这一点,这是显而易见的。非常感谢@Knownasilya!一旦你指出这一点,这是显而易见的。