自定义验证程序抛出RuntimeError ::属性不能为空

时间:2012-05-22 19:14:38

标签: ruby-on-rails ruby-on-rails-3 validation customvalidator

在我的Rails应用程序中,我有一个Like模型。

### like.rb
### Custom Validator Code:
class UniquenessValidator < ActiveModel::Validator
  def validate(record)
      # Empty   
  end
end

class Like < ActiveRecord::Base
  include ActiveModel::Validations
  validates_with UniquenessValidator

  attr_accessible :user_id

  belongs_to :likeable, polymorphic: true
  belongs_to :user
end

在我的rails控制台中,我尝试做Like.all(目前我的Likes表是空的)

1.9.2p320 :001 > Like.all
RuntimeError: :attributes cannot be blank
        from /Users/user/.rvm/gems/ruby-1.9.2-p320@test_app/gems/activemodel-3.2.3/lib/active_model/validator.rb:141:in `initialize'
        from /Users/user/.rvm/gems/ruby-1.9.2-p320@test_app/gems/activerecord-3.2.3/lib/active_record/validations/uniqueness.rb:7:in `initialize'
        from /Users/user/.rvm/gems/ruby-1.9.2-p320@test_app/gems/activemodel-3.2.3/lib/active_model/validations/with.rb:84:in `new'
        from /Users/user/.rvm/gems/ruby-1.9.2-p320@test_app/gems/activemodel-3.2.3/lib/active_model/validations/with.rb:84:in `block in validates_with'
        from /Users/user/.rvm/gems/ruby-1.9.2-p320@test_app/gems/activemodel-3.2.3/lib/active_model/validations/with.rb:83:in `each'
        from /Users/user/.rvm/gems/ruby-1.9.2-p320@test_app/gems/activemodel-3.2.3/lib/active_model/validations/with.rb:83:in `validates_with'
        from /Users/user/Programming/WWW/Rails/experiments/test_app/app/models/like.rb:3:in `<class:Like>'
        from /Users/user/Programming/WWW/Rails/experiments/test_app/app/models/like.rb:1:in `<top (required)>'
        from /Users/user/.rvm/gems/ruby-1.9.2-p320@test_app/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:469:in `load'
        from /Users/user/.rvm/gems/ruby-1.9.2-p320@test_app/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:469:in `block in load_file'
        from /Users/user/.rvm/gems/ruby-1.9.2-p320@test_app/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:639:in `new_constants_in'
        from /Users/user/.rvm/gems/ruby-1.9.2-p320@test_app/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:468:in `load_file'
        from /Users/user/.rvm/gems/ruby-1.9.2-p320@test_app/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:353:in `require_or_load'
        from /Users/user/.rvm/gems/ruby-1.9.2-p320@test_app/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:502:in `load_missing_constant'
        from /Users/user/.rvm/gems/ruby-1.9.2-p320@test_app/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:192:in `block in const_missing'
        from /Users/user/.rvm/gems/ruby-1.9.2-p320@test_app/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in `each'
        from /Users/user/.rvm/gems/ruby-1.9.2-p320@test_app/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in `const_missing'
        from (irb):1
        from /Users/user/.rvm/gems/ruby-1.9.2-p320@test_app/gems/railties-3.2.3/lib/rails/commands/console.rb:47:in `start'
        from /Users/user/.rvm/gems/ruby-1.9.2-p320@test_app/gems/railties-3.2.3/lib/rails/commands/console.rb:8:in `start'
        from /Users/user/.rvm/gems/ruby-1.9.2-p320@test_app/gems/railties-3.2.3/lib/rails/commands.rb:41:in `<top (required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'
1.9.2p320 :002 > 

这里发生了什么? (顺便说一下,如果我从validates_with UniquenessValidator删除Like.rb,我就不会收到此错误)

2 个答案:

答案 0 :(得分:1)

调用验证器MyUniquenessValidator。

在activerecord中

UniquenessValidator已存在。这个字符串from /Users/user/.rvm/gems/ruby-1.9.2-p320@test_app/gems/activerecord-3.2.3/lib/active_record/validations/uniqueness.rb:7:in initialize'`告诉你它。

小心预定义的ruby和RoR类(数据类型(例如,“Complex”),rails验证器等)。

答案 1 :(得分:0)

我知道这不是你的情况,但我收到了这个错误,因为我意外地从EveryValidator而不是Validator继承了。

是:

class UserProfileValidator < ActiveModel::EachValidator
  def validate(record)
  end
end

应该是:

class UserProfileValidator < ActiveModel::Validator
  def validate(record)
  end
end

或者:

class UserProfileValidator < ActiveModel::EachValidator
  def def validate_each(record, attribute, value)
  end
end