Ruby on rails Rails验证是否满足条件
我需要你的帮助来验证一个简单的Rails模型 首先,我想检查用户是否填写了所有输入字段:Ruby on rails Rails验证是否满足条件,ruby-on-rails,ruby-on-rails-4,conditional,Ruby On Rails,Ruby On Rails 4,Conditional,我需要你的帮助来验证一个简单的Rails模型 首先,我想检查用户是否填写了所有输入字段: class Test < ActiveRecord::Base validates :firstname, :lastname, :firstvalue, :secondvalue, presence: true [...] 好吧,这是可行的,但只有在所有其他字段都被填充的情况下!创建一个新条目,将所有字段留空,我得到一个未定义的方法您可以这样做 validate :biggerTh
class Test < ActiveRecord::Base
validates :firstname, :lastname, :firstvalue, :secondvalue, presence: true
[...]
好吧,这是可行的,但只有在所有其他字段都被填充的情况下!创建一个新条目,将所有字段留空,我得到一个未定义的方法您可以这样做
validate :biggerThan, if: Proc.new{ |test| test.firstvalue.present? and test.secondvalue.present? }
如果您还添加了数值性
验证,那就太好了
validates :firstvalue, numericality: true
validates :secondvalue, numericality: true
validates :firstvalue, numericality: true
validates :secondvalue, numericality: true