2

validates_confirmation_of我的 Rails 应用程序 似乎坏了。

我的模型如下所示:

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

1 回答 1

4

Rails 文档指出:

“注意:仅当 password_confirmation 不为零时才执行此检查,并且默认情况下仅在保存时执行。要要求确认,请确保为确认属性添加存在检查:”

因此,为您的班级添加存在检查,例如

class Blark
  include ActiveModel::Validations
  attr_accessor :text
  validates_confirmation_of :text
  validates_presence_of :text, :text_confirmation
end
于 2012-06-16T14:28:06.167 回答