Rails 3自定义验证器问题 [英] Rails 3 Custom Validator Problem
问题描述
我需要对模型应用验证,使记录中的2个整数值,最小值和最大值形成一个包含范围(例如2和3是确定的,但4和1不是)。从我的理解,因为我需要验证在同一记录中的两个值,我必须使用ActiveModel :: Validator(而不是ActiveModel :: EachValidator)。所以,我尝试以下:
模型(app / models / position.rb):
class Position< ActiveRecord :: Base
validates_with InclusiveRangeValidator
end
验证器(app / lib / validators / inclusive_range_validator.rb):
class InclusiveRangeValidator< ActiveModel :: Validator
def validate(record)
record.errors [:base]< #{record.minimum}和#{record.maximum}不构成有效的包含范围。除非记录record.maximum
end
end
我读过Rails 3.0。 5不会自动加载lib目录,所以我在config / application.rb中添加了这一行:
config.autoload_path + =%W({config.root} / lib)
然后我重置rails服务器它会改变到config / application.rb。我运行我的单元测试,试图锻炼模型来证明这种验证工作。我收到此错误:
未初始化的常量:Position :: InclusiveRangeValidator(NameError)
我认为发生的是Rails不能识别/找到我的自定义验证器类,因此它假定InclusiveRangeValidator是它引用的类中的一个常量但是,我认为我对config / application.rb的更改会将我的验证器放在加载路径,以便它可用。
我已经走了通过StackOverflow上的其他帖子,没有提出一个解决方案,我已经阅读API文档验证器,无济于事。我必须做一些简单而愚蠢的事情,但我看不出问题是什么。任何帮助?
EDIT :
经过更多搜索,我发现我根本不需要自定义验证器我可以用这个完成同样的目标:validates:minimum,:numericality => {:greater_than_or_equal_to => 0}
validates:maximum,:numericality => {:greater_than => :minimum}
但是,Rails无法找到自定义验证类。
解决方案一次,我将application.rb中的行更改为:
config.autoload_paths + =%W [#{config.root} / lib / validators /]
Rails能够找到加载我的自定义验证器的正确路径。我犯了一个错误,假设Rails会自动递归目录结构,这显然不是这样。
I need to apply validation to a Model so that 2 integer values in the record, minimum and maximum, form an inclusive range (ex. 2 and 3 are ok, but 4 and 1 are not). From what I understand, since I need to validate 2 values against each other in the same record, I have to use ActiveModel::Validator (and not ActiveModel::EachValidator). So, I try the following:
Model (app/models/position.rb):
class Position < ActiveRecord::Base validates_with InclusiveRangeValidator end
Validator (app/lib/validators/inclusive_range_validator.rb):
class InclusiveRangeValidator < ActiveModel::Validator def validate(record) record.errors[:base] << "#{record.minimum} and #{record.maximum} do not form a valid inclusive range." unless record.minimum < record.maximum end end
I've read that Rails 3.0.5 doesn't automatically load the lib directory anymore, so I added this line in config/application.rb:
config.autoload_path += %W({config.root}/lib)
And then I reset the rails server so it'll take the change to config/application.rb. I run my unit tests, which tries to exercise the model to prove this validation works. I get this error:
uninitialized constant: Position::InclusiveRangeValidator (NameError)
What I think is happening is that Rails is not recognizing/finding my custom validator class, and so it assumes InclusiveRangeValidator is a constant in the class that it's referenced in. But, I thought the change I made to config/application.rb would put my validator in the load path so that it would be available.
I've gone through the other posts on StackOverflow and didn't come up with a solution, and I've read the API docs on validators, to no avail. I've got to be doing something simple and stupid, but I can't see what the issue is. Any help?
EDIT: After more searching, I discovered that I don't need a custom validator at all, as I can accomplish the same goal with this:
validates :minimum, :numericality => {:greater_than_or_equal_to => 0 } validates :maximum, :numericality => {:greater_than => :minimum }
However, the question still remains as to why Rails can't locate the custom validation class.
解决方案Once, I changed the line in application.rb to:
config.autoload_paths += %W[#{config.root}/lib/validators/]
Rails was able to find the right path to load my custom validator. I made the mistake of assuming Rails would automatically recurse the directory structure, this is evidently not the case.
这篇关于Rails 3自定义验证器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!