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

私はRubyonRailsの初心者です。このチュートリアルに合格しました。私がやっているとき、私はあなたと同じエラーを見つけました。

私の解決があなたを助けることができることを願っています。

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)「end」タグを見逃しました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 に答える