2017-12-28 3 views
2

나는 Michael Hartl의 Ruby on Rail 책을 계속 따라 가고 있으며 사용자가 자신의 프로필 설정을 편집 할 수있게하려고 10 장에 있습니다. 이것은 가장 이상한 오류입니다 - bcrypt의 password_digest는 방해 행위입니다. 내 검사들.Has_secure_password Bcrypt가 nil 패스워드를주는 것

users.yml이 내가

user.valid을 볼 것입니다

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

내가 레일 콘솔이 실행? = "거짓

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 : 테이블 "users"에 "password"라는 컬럼이 없다. –

+0

테스트 코드는 어떻습니까? – Daniel

답변

0

user: 
    name: Example User 
    email: [email protected] 
    password: "password" 

Bcrypt는 (그것을를 업데이트하는 동안 옵션) 새 레코드를 작성하는 동안 passwordattr_accessor이 존재한다는 것을 검증이있는 아래 코드를 사용해보십시오. 길이 검증이 필요하지 않기 때문에 단지 YML에서`password`를 제공하려고 암호 자체가 빈/전무

관련 문제