2017-12-28 19 views
2

私はMichael HartlのRuby on Railの本を踏襲しようとしています。私は第10章で、ユーザーがプロファイル設定を編集できるようにしようとしています。これは最も奇妙なエラーです - bcryptのpassword_digestは、私のテスト。私はレールコンソールでこれを実行するとHas_secure_passwordパスワードなしのBcrypt

user: 
    name: Example User 
    email: [email protected] 
    password_digest: <%= User.digest("password") %> 

users.ymlが、これは私が

user.valid見るものでしょうか? =>

user.errors.full_messages =>[ "パスワードは空白にすることはできません"、 "パスワードが短すぎる(最小6文字)"]

user.rb

class User < ApplicationRecord 
    VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i 
    before_save { 
    self.email.downcase! 
    } 

    validates :name, presence: true, length: { maximum: 50 } 
    validates :email, presence: true, length: { maximum: 255 }, 
      format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false } 
    validates :password, presence: true, length: { minimum: 6 } 
    has_secure_password 

    # Returns the hash digest of the given string. 
    def User.digest(string) 
    cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : 
       BCrypt::Engine.cost 
    BCrypt::Password.create(string, cost: cost) 
    end 
end 

私はbcryptのバージョン 'に3.1.11'

を使用しています

私のテストコード

def setup 
    @user = users(:marko) 
    end 

    test "successful edit" do 
    log_in_as(@user) 
    get edit_user_path(@user) 
    name = "Foo bar" 
    email = "[email protected]" 
    patch user_path(@user), params: { user: { 
              name: name, 
              email: email, 
              password: "password", 
              password_confirmation: "password" } } 
    assert_not flash.empty? 
    assert_redirected_to @user 
    follow_redirect! 
    @user.reload 
    assert_equal name, @user.name 
    assert_equal email, @user.email 
    end 

間違っているかもしれないものの任意のアイデア?

+0

とき

また、パスワードのあなたのlength validationallow_blank: trueを追加する必要があります。ダイジェストは自動的に作成されます。 –

+0

これを試してみましたが、 'ActiveRecord :: Fixture :: FixtureError:table" users "に" password "という名前の列がありません。 –

+0

テストコードはどのようになっていますか? – Daniel

答えて

関連する問題