首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ArgumentError在Michael的"Rails教程“第7章中

ArgumentError在Michael的"Rails教程“第7章中
EN

Stack Overflow用户
提问于 2011-03-06 15:00:59
回答 1查看 578关注 0票数 0

所有人。我遇到了以下问题:在实现has_password之后?在7.2.3节中,RSpec显示了以下错误,例如“应该创建一个给定有效属性的新实例”测试

(

1)用户应该创建一个新实例,给出有效的属性: User.create!(@attr) ArgumentError:参数的错误数量(1表示0) #./app/model/user.rb:42: in secure_hash' # ./app/models/user.rb:39:inmake_salt‘#./app/model/user.rb:30:in encrypt_password' # ./spec/models/user_spec.rb:14:inblock (2个级别)

完成在1.47秒1例,1失败<-从(1)运行完成!

我不明白到底是什么导致了这个问题。以下是我的user.rb代码:

代码语言:javascript
复制
    require 'digest'

class User < ActiveRecord::Base 
  attr_accessor :password
  attr_accessible :name, :email, :password, :password_confirmation

  email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

  validates :name,  :presence => true,
                    :length   => { :maximum => 50 }
  validates :email, :presence => true,
                     :format   => { :with => email_regex },
                     :uniqueness => { :case_sensitive => false }

  # Automatically create the virtual attribute "password_confirmation"
  validates :password, :presence => true,
                    :confirmation => true,
                    :length => { :within => 6..40 } 

  before_save :encrypt_password

  # Return 'true' if the user's password matches the submitted password
  def has_password?(submitted_password)
    encrypted_password == encrypt(submitted_password)
  end

  private

  def encrypt_password
    self.salt = make_salt if new_record?
    self.encrypted_password = encrypt(password)
  end

  def encrypt(string)
    secure_hash("#{salt}--#{string}")
  end

  def make_salt
    secure_hash("#{Time.now.utc}--#{password}")
  end

  def secure_hash
    Digest::SHA2.hexdigest(string)
  end
end

它能是什么?提前谢谢你!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-03-06 15:30:47

您的secure_hash方法需要使用一个参数。

代码语言:javascript
复制
def secure_hash(string)
  Digest::SHA2.hexdigest(string)
end
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5211210

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档