與rails 4我使用has_secure_password在我的用戶模型中,技巧說如果我不設置:password_confirmation它永遠不會被觸發,但爲什麼當我運行測試時我得到錯誤:確認密碼不能爲空如下所示:rails 4 has_secure_password渲染密碼確認可選
Failures:
1) User
Failure/Error: it { should be_valid }
expected #<User id: nil, name: "joe", email: "[email protected]", created_at: nil,
updated_at: nil, password_digest: "$2a$04$mcRr/msgYQR3kBVc3kv/m.UotBJuJuSXZKMw
/eHTvU87..."> to be valid, but got errors: Password confirmation can't be blank
我的測試文件的樣子:
require 'spec_helper'
describe User do
before { @user = User.new(name: 'joe', email: '[email protected]', password: 'foo') }
subject { @user }
#....
#....
describe "when password is not present" do
before { @user.password = "" }
it { should_not be_valid }
end
end
爲什麼我得到這個錯誤,有作爲解決方案?感謝的
,但我不會增加password_confirmation到我的應用程序,我只需要輸入密碼一次 – medBo
它僅在用戶創建期間使用。您不必(也不應該)將其添加到您的模型中。這是'has_secure_password'的一個特性。 –
我不明白,請你能解釋更多! (在我的形式中,我認爲字段的密碼,而不是另一個密碼確認) – medBo