Michael Hartl 的 Rails 3 教程:has_password 有问题?第 7 章中的方法 [英] Michael Hartl's Rails 3 Tutorial: Problem with has_password? method in Chapter 7

查看:40
本文介绍了Michael Hartl 的 Rails 3 教程:has_password 有问题?第 7 章中的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这让我整个上午都在苦恼.

This has been kicking my butt all morning.

现在,我刚刚开始阅读 Michael Hartl 出色的Ruby on Rails 3 教程的第 7.2.4 章,我遇到了一些问题.本节首先快速检查 Rails 控制台沙箱中的 has_password? 方法.这是我输入的内容:

Right now, I'm just beginning Chapter 7.2.4 of Michael Hartl's excellent Ruby on Rails 3 Tutorial, and I'm running into some issues. The section begins with a quick check of the has_password? method, in the Rails console sandbox. Here's what I've typed in:

ruby-1.9.2-p180 :001 > User
=> User(id: integer, name: string, email: string, created_at: datetime, updated
updated_at: datetime, encrypted_password: string, salt: string) 
ruby-1.9.2-p180 :002 > User.create(:name => "John Pavlick", :email => "jmpavlick
@gmail.com", :password => "foobar", :password_confirmation => "foobar")
 => #<User id: nil, name: "John Pavlick", email: "jmpavlick@gmail.com", created_
at: nil, updated_at: nil, encrypted_password: nil, salt: nil> 
ruby-1.9.2-p180 :003 > user = User.find_by_email("jmpavlick@gmail.com"
ruby-1.9.2-p180 :004?>   )
 => #<User id: 1, name: "John Pavlick", email: "jmpavlick@gmail.com", created_at
: "2011-04-15 15:11:46", updated_at: "2011-04-15 15:11:46", encrypted_password:
nil, salt: nil>
ruby-1.9.2-p180 :005 > user.has_password?("foobar")
 => false

这应该返回 true.

这是我的 user.rb 模型:

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

    email_regex = /^[\w+\-.]+@[a-z\d\-.]+\.[a-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(string)
          Digest::SHA2.hexdigest(string)
        end
end

我所有的 RSpec 测试都完美通过,据我所知,我已经逐字编码了书中的所有内容 - 我什至复制/粘贴了一些代码以确保.我看不出有任何理由会失败,因此任何帮助都将是救命稻草.

All of my RSpec tests pass perfectly, and as far as I can tell, I've coded in verbatim everything in the book - I've even copy/pasted some code to make sure. I don't see any reason at all for this to be failing, so any help will be a lifesaver.

这是该书的在线链接:[link]

推荐答案

在您的 create 调用之后,请注意返回的对象没有 id 或加密密码...我怀疑验证失败并且记录没有正在创建,也许是因为您已经有一个记录?

After your create call, notice that the object returned does not have a id or encrypted password... I suspect that a validation is failing and the record is not being created, perhaps because you already have a record present?

这篇关于Michael Hartl 的 Rails 3 教程:has_password 有问题?第 7 章中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆