使用 validates_presence_of 和 validates_numericality_of 时验证失败

发布于 2024-12-14 14:51:11 字数 1032 浏览 0 评论 0原文

我需要验证模型中的数据,然后使用 rspec 测试该模型:

这是模型中的验证:

validates_presence_of :sales_price
validates_presence_of :retail_price

validates_numericality_of :sales_price, :greater_than => 0
validates_numericality_of :retail_price,:greater_than => 0
validates_numericality_of :sales_price, :less_than => :retail_price,
                                          :message => "must be less than retail price."

当我尝试像这样测试模型时,

it { should validate_presence_of :sales_price }
it { should validate_presence_of :retail_price }
it { should validate_numericality_of :sales_price }
it { should validate_numericality_of :retail_price }

我收到此错误

Failure/Error: it { should validate_presence_of :retail_price }
 ArgumentError:
   comparison of Float with nil failed
 # ./spec/models/offer_option_spec.rb:19:in `block (2 levels) in <top (required)>'

我该如何解决这个问题?

I need to validate data in my model and then test this model using rspec:

Here is the validation in model:

validates_presence_of :sales_price
validates_presence_of :retail_price

validates_numericality_of :sales_price, :greater_than => 0
validates_numericality_of :retail_price,:greater_than => 0
validates_numericality_of :sales_price, :less_than => :retail_price,
                                          :message => "must be less than retail price."

when i'm trying to test tho model like this

it { should validate_presence_of :sales_price }
it { should validate_presence_of :retail_price }
it { should validate_numericality_of :sales_price }
it { should validate_numericality_of :retail_price }

I get this error

Failure/Error: it { should validate_presence_of :retail_price }
 ArgumentError:
   comparison of Float with nil failed
 # ./spec/models/offer_option_spec.rb:19:in `block (2 levels) in <top (required)>'

How can i solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帥小哥 2024-12-21 14:51:11

谢谢您的回答。

我终于明白了。

validates_numericality_of :sales_price, :greater_than => 0,
                        :allow_blank => true
validates_numericality_of :retail_price, :greater_than => 0,
                        :allow_blank => true
validates_numericality_of :sales_price, :less_than => :retail_price,
                        :if => Proc.new { |o| !o.retail_price.nil? } ,
                        :message => "can't be greater than retail price."

现在我遇到了另一个问题。我正在使用rails.validation.js,它可以帮助我执行客户端验证。如果您在数值验证器中使用类似的内容,一切都可以:

:greater_than => 0

validation.js 创建一个函数来检查字段中的值是否大于 0。该函数的创建方式如下:

new Function("return " + element.val() + CHECKS[check] + options[check])()))

element 是我的输入字段,
CHECKS 包含不同的哈希值:less_than : <、greater_than : > 等
options[check] 包含一些传递到 validator 的值(:greater_than => 0,在本例中为零,options[check] is less_than : '0')。但是,当我使用其他内容而不是值时,我收到错误:

Uncaught ReferenceError: retail_price is not defined 

我尝试了类似的操作

validates_numericality_of :sales_price, :less_than => Proc.new{ self.retail_price },
                        :if => Proc.new { |o| !o.retail_price.nil? } ,
                        :message => "can't be greater than retail price."

,但执行验证时该对象不存在,因此 self 只是指向该类,而 Retail_price 不存在。

你会建议什么来解决这个问题?

Thank you for your answer.

I finally got this.

validates_numericality_of :sales_price, :greater_than => 0,
                        :allow_blank => true
validates_numericality_of :retail_price, :greater_than => 0,
                        :allow_blank => true
validates_numericality_of :sales_price, :less_than => :retail_price,
                        :if => Proc.new { |o| !o.retail_price.nil? } ,
                        :message => "can't be greater than retail price."

Now i got another problem. I'm using rails.validation.js which helps me to perform client side validations. Everything is ok if you use something like this in your numerical validator:

:greater_than => 0

validation.js creates a function that checks if the value in the field is greater than 0. The function is created like that:

new Function("return " + element.val() + CHECKS[check] + options[check])()))

element is my input field,
CHECKS contains different hash: less_than : <, greater_than : >, etc
options[check] contains some value that is passed into validator(:greater_than => 0, zero in this case, options[check] is less_than : '0'). But when i use something else instead of a value i get an error:

Uncaught ReferenceError: retail_price is not defined 

I tried something like this

validates_numericality_of :sales_price, :less_than => Proc.new{ self.retail_price },
                        :if => Proc.new { |o| !o.retail_price.nil? } ,
                        :message => "can't be greater than retail price."

but the object doesn't exist when the validation is performed, so self just points to the class and retail_price doesn't exist.

what would you recommend to solve the problem?

童话里做英雄 2024-12-21 14:51:11

@sled 是正确的,nil 检查内置于 validates_numericality_of 中并默认启用,因此没有必要同时满足这两个要求。

另一件需要注意的事情是,销售价格实际上验证了两次数值,这可能会导致问题。我会把它改为

validates_numericality_of :sales_price, :greater_than => 0, :less_than => :retail_price, :message => "must be less than retail price."

@sled is correct that the nil check is built into validates_numericality_of and is enabled by default, so it's unnecessary to have both requirements.

One other thing to note is that sales price really is validating numericality twice which might be causing a problem. I would instead change it to

validates_numericality_of :sales_price, :greater_than => 0, :less_than => :retail_price, :message => "must be less than retail price."
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文