0

我正在做 ruby​​ on rails 教程书,但我遇到了一个错误。我检查了他写的所有内容,但我仍然得到错误。它告诉我身份验证是 nil:NILclass 的未定义方法。我不知道该怎么办。

用户.rb:

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

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, presence: true, length:{ minimum: 6 }
validates :password_confirmation, presence: true
end

和(一些)我的 user_spec.rb

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

    subject { @user }

it { should respond_to(:password_digest) }
it { should respond_to(:password) }
it { should respond_to(:password_confirmation) }
it { should respond_to(:authenticate) }

it { should be_valid }

       #    USER.PASSWORD

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

describe "when password is not present" do
  before { @user.password_confirmation = "mismatch" }
  it { should_not be_valid }

describe "when password is nil" do
  before { @user.password_confirmation = nil }
  it {should_not be_valid}
end  

describe "with a password that's too short" do
  before { @user.password = @user.password_confirmation = "a" * 5 }
  it { should be_valid }
end

describe "return value of authenticate method" do
  before { @user.save }
  let(:found_user) { User.find_by_email(@user.email) }

  describe "with valid password" do
    it { should == found_user.authenticate(@user.password) }
    end

  describe "with invalid password" do
    let(:user_for_invalid_password) { found_user.authenticate("invalid") }

    it {should_not == user_for_invalid_password}
    specify { user_for_invalid_password.should be_false}
  end
end
end

非常感谢您提供的所有帮助。

4

1 回答 1

1

我是 ruby​​ on rails 的新手。我刚刚通过了这个教程。当我这样做时,我发现与您相同的错误。

希望我的解决方法可以帮到你。

在您的 user.rb
删除行中: attr_accessor 并将 has_secure 密码编辑为 has_secure_password (添加下划线)

class User < ActiveRecord::Base 
attr_accessible :email, :name, :password, :password_confirmation
has_secure_password
enter code here
  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, presence: true, length:{ minimum: 6 }
  validates :password_confirmation, presence: true
end

在你的 user.spec.rb

1)你错过了“结束”标签describe "when password is not present"

2)将测试describe 'with a password that's too short' 从更改it { should be_valid}it { should be_invalid } 我认为如果密码小于5应该是无效的。

3)最后,如果您想测试通过身份验证,您应该使用 .com 或任何与 user.rb 中的 VALID_EMAIL_REGEX 匹配的电子邮件结尾,例如:

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

我希望这对你有帮助。

对不起,我的英语不好。

于 2013-02-18T15:58:25.730 回答