当失败时,验证正在通过--Michael Hartl的RoR,6.2.2

时间:2012-12-23 18:56:04

标签: ruby-on-rails-3 validation

我正在尝试向我的用户模型添加验证,但数据不会失败,而是继续传递。对于名字,我没有参赛作品。见下文:

1.9.3p286 :012 > user = User.new(name: "", email: "fail@fail.com")
  => #<User id: nil, name: "", email: "fail@fail.com", created_at: nil, updated_at: nil> 

1.9.3p286 :013 > user.save
   (0.1ms)  SAVEPOINT active_record_1
  SQL (1.5ms)  INSERT INTO "users" ("created_at", "email", "name", "updated_at") VALUES    (?, ?, ?, ?)  [["created_at", Sun, 23 Dec 2012 18:52:00 UTC +00:00], ["email",   "fail@fail.com"], ["name", ""], ["updated_at", Sun, 23 Dec 2012 18:52:00 UTC +00:00]]
   (0.1ms)  RELEASE SAVEPOINT active_record_1
 => true 

以下是app/models/user.rb中的内容:

class User < ActiveRecord::Base
  attr_accessible(:name, :email)

  validates(:name, presence: true)
end

这应该是失败的。有什么建议?谢谢!

1 个答案:

答案 0 :(得分:0)

它应该可以工作,我想你在添加验证后没有重新加载控制台