应该:validates_each 出现未知错误
我很难理解我的验证方法出了什么问题。
我安装了shoulda,当我尝试使用它时,它出现了一个奇怪的未定义方法 NoMethodError: undefined method '[]' for false:FalseClass
的错误,这非常奇怪。
我将其范围缩小到这段有问题的代码:
validates_each :name do |record, attr, value|
name_hash = self.parse_name(value)
record.errors.add attr, 'must have a first and last name' if
( name_hash[:first_name].nil? || name_hash[:last_name].nil? )
end
您可以在 gist-596202
任何见解将不胜感激。
I'm having an awful time trying to understand what my validation method is having trouble with.
I installed shoulda and when I tried to use it it errors out with a strange undefined method NoMethodError: undefined method '[]' for false:FalseClass
which is very odd.
I narrowed it down to this piece of offending code:
validates_each :name do |record, attr, value|
name_hash = self.parse_name(value)
record.errors.add attr, 'must have a first and last name' if
( name_hash[:first_name].nil? || name_hash[:last_name].nil? )
end
You can see the full error, offending model (simplified), the test case, and environment info at gist-596202
Any insight would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
parse_name
方法以这一行开头:这表明当您尝试验证该值时,该值不是字符串(可能为 nil)。
validate_presence_of
的 shoulda 匹配器将使用 nil 值对其进行测试,这就是您失败的原因。Your
parse_name
method starts with this line:This would indicate that the value is not a string (probably nil) when you're trying to validate it. The shoulda matcher for
validate_presence_of
will test it with a nil value, which is why you're getting the failure.