2012-06-16 64 views
2

我似乎在我的rails應用程序中破壞了validates_confirmation_of我似乎已經破解了validates_confirmation_of

我的模型看起來是這樣的:

class Blark 
    include ActiveModel::Validations 
    attr_accessor :text 
    validates_confirmation_of :text 
end 

這是當我使用它會發生什麼:

0 HAL work/nrb-brewery-management % rails c 
Loading development environment (Rails 3.2.5) 

1.9.3p0 :001 > b = Blark.new 
=> #<Blark:0xae2e2d0> 

1.9.3p0 :002 > b.text = 'llama' 
=> "llama" 

1.9.3p0 :003 > b.text_confirmation 
=> nil 

1.9.3p0 :004 > b.valid? 
=> true 

爲什麼b有效嗎?

回答

4

Rails documentation狀態:

「注意:此檢查僅password_confirmation不爲零,默認情況下只保存進行到需要確認,確保添加存在確認的確認屬性。」

因此,添加一個存在檢查你的班級,例如

class Blark 
    include ActiveModel::Validations 
    attr_accessor :text 
    validates_confirmation_of :text 
    validates_presence_of :text, :text_confirmation 
end 
相關問題