1

我是 ROR 的新手,正在完成 Lynda.com 教程。我已经完成了有关身份验证的部分,wrong number of arguments当我尝试在 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'

这是来自我的 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 上的所有类似问题,但似乎无法理解我应该如何解决它,但我会继续尝试。;-)

4

1 回答 1

1

看起来您忘记添加第二个参数salt

def self.hash_with_salt(password="")
                                  ^

这就是为什么您在调用时遇到错误的原因:

self.hashed_password = AdminUser.hash_with_salt(password, salt)
于 2012-12-29T17:03:37.083 回答