LoginSignup
0
1

More than 3 years have passed since last update.

passwordとpassword_confirmation in rails チュートリアル

Posted at

この記事はRailsチュートリアル6章の内容です。

2つの属性はhas_secure_passwordメソッドをモデルで呼び出した時に

自動的に追加される仮想的な属性です。

また、値が一致するかどうかのvalidationが追加されます。

app/models/user.rb
 has_secure_password

この状態でテストを行うとredになります。

なぜなら、@userでpasswordとpassword_confirmationの値が設定されていないからです。

def setup
  @user = User.new(name: "Example User", email: "user@example.com")
end

なので、以下のように書き換えます。

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

passwordとpassword_confirmationに同じ値を代入することでテスト結果がgreen
になります。

以上です。

rails チュートリアル6章

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1