如果条件在 rails 3.2 和 mongoid + simple_form 上,则validates_presence_of [英] validates_presence_of if condition on rails 3.2 and mongoid + simple_form

查看:29
本文介绍了如果条件在 rails 3.2 和 mongoid + simple_form 上,则validates_presence_of的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果属性 :shipping 等于 true<,我想验证这两个属性 :shipping_cost:shipping_cost_anywhere 是否存在/代码>.如果

I want validate presence of these 2 attributes :shipping_cost and :shipping_cost_anywhere if the attribute :shipping is equal to true. and If

我的模型中有这个,但对我来说效果不佳:

I have this in my model but not working fine for me:

validates_presence_of :shipping_cost, :shipping_cost_anywhere, :allow_blank => "true" if :shipping == "true"

这是我的 :shipping 属性:

this is my :shipping attribute:

field :shipping, :type => Boolean, :default => "false"

我该怎么做?

谢谢!

已编辑.

我正在使用 mongoid 和 simple_form gems

推荐答案

我对这个问题的修复是下一个代码:

The fix for me to this question is the next code:

validates :shipping_cost, :shipping_cost_anywhere, :presence => true, :if => :shipping?

感谢大家的帮助,但任何答案都对我有用.谢谢!

Thank you to all for your help but any answer has worked for me. thanks!

这篇关于如果条件在 rails 3.2 和 mongoid + simple_form 上,则validates_presence_of的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆