1

我一直关注 Michael Hartl 的http://ruby.railstutorial.org直到第 6.3.2 章,在那里我停留在密码和确认。(http://ruby.railstutorial.org/chapters/modeling-users#sec:adding_a_secure_password

在阅读了以下说明后,我根据我的理解将“attr_assessor :password, :password_confirmation”添加到了 User.rb 中:

“如图 6.1 中的模型所示,我们希望用户确认他们的密码,这是网络上的一种常见做法,旨在最大程度地减少拼写错误。我们可以在控制器层强制执行此操作,但将其放入模型中并使用是常规做法Active Record 强制约束。方法是在 User 模型中添加 password 和 password_confirmation 属性,然后要求这两个属性匹配才能将记录保存到数据库中。与我们目前看到的其他属性不同,密码属性将是虚拟的——它们只会暂时存在于内存中,不会持久化到数据库中。”

添加后,我在 user_spec.rb 上的 bundle exec guard 上出现 11 次失败:

password: "foobar", password_confirmation: "foobar" to @User.new in models/user_spec.rb

类似于 db:development.sqlite3 中不存在的“虚拟”属性(密码、密码确认)。这就是我试图做的没有成功的事情。我什至尝试了所有可能的方法,例如在 user.rb 中使用 @User 的哈希

我在这里做错了什么?

提前致谢

file: spec/user_spec.rb
require 'spec_helper'

describe User do

  before do
    # @user = User.new(name: "Example User", email: "user@example.com")
    @user = User.new(name: "Example User", email: "user@example.com",
                    password: "foobar", password_confirmation: "foobar")
  end

  subject { @user }

  it { should respond_to(:name) }
  it { should respond_to(:email) }
  it { should respond_to(:password_digest) }
  it { should respond_to(:password)}
  it { should respond_to(:password_confirmation) }

  describe "when name is not present" do
    before { @user.name = " " }
    it { should_not be_valid }
  end

  describe "when email is not present" do
    before { @user.email = " " }
    it { should_not be_valid }
  end

  describe "when name is too long" do
    before { @user.name = "a" * 51 }
    it { should_not be_valid }
  end

  describe "when email format is invalid" do
    it "should be invalid" do
      addresses = %w[user@foo,com user_at_foo.org example.user@foo.]
      addresses.each do |invalid_address|
        @user.email = invalid_address
        @user.should_not be_valid
      end      
    end
  end

  describe "when email format is valid" do
    it "should be valid" do
      addresses = %w[user@foo.com A_USER@f.b.org frst.lst@foo.jp a+b@baz.cn]
      addresses.each do |valid_address|
        @user.email = valid_address
        @user.should be_valid
      end      
    end
  end

  describe "when email address is already taken" do
    before do
      user_with_same_email = @user.dup
      user_with_same_email.save
    end

    it { should_not be_valid }
  end
end

.

file: models/user.rb
# == Schema Information
#
# Table name: users
#
#  id              :integer         not null, primary key
#  name            :string(255)
#  email           :string(255)
#  created_at      :datetime        not null
#  updated_at      :datetime        not null
#  password_digest :string(255)
#

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

  before_save { |user| user.email = email.downcase }

  validates :name, presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true,
            format: { with: VALID_EMAIL_REGEX },
            uniqueness: { case_sensitive: false }
  #validates :password, length: { minimum: 6 }
  #validates :password_confirmation, presence: true

end
4

3 回答 3

3

如果您只在第 6.3.2 节上,那么测试应该会失败。您将在以下部分中更正该问题。

于 2012-04-28T21:52:12.827 回答
2

将 has_secure_password 添加到模型中可以解决问题。它也在书中(后半部分)。

于 2012-09-13T03:03:20.237 回答
0

我猜这些错误是在抱怨“找不到表‘用户’”试试:

rake db:migrate 
rake db:load:test
于 2012-04-28T21:41:14.030 回答