Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 唯一未定义的方法错误轨道_Ruby On Rails_Methods_Undefined - Fatal编程技术网

Ruby on rails 唯一未定义的方法错误轨道

Ruby on rails 唯一未定义的方法错误轨道,ruby-on-rails,methods,undefined,Ruby On Rails,Methods,Undefined,我在Rails中有一个非常独特的“未定义方法错误”案例。我有一个任务订单模型,它的属性是“responsibledamount”和“awardAmount”。在创建新任务订单时,我的业务规则之一是“义务金额”不能大于“奖励金额”。因此,确保这一点,我进行了自定义验证: validate :check_amount_obilgated validates_presence_of :awardAmount validates_presence_of :obligatedAmount def ch

我在Rails中有一个非常独特的“未定义方法错误”案例。我有一个任务订单模型,它的属性是“responsibledamount”和“awardAmount”。在创建新任务订单时,我的业务规则之一是“义务金额”不能大于“奖励金额”。因此,确保这一点,我进行了自定义验证:

validate :check_amount_obilgated
validates_presence_of :awardAmount
validates_presence_of :obligatedAmount


def check_amount_obilgated #cannot be greater than contract award amount
    if obligatedAmount > awardAmount
        errors.add(:obligatedAmount, "The Obligated Amount cannot be greater than the Award Amount")
    end
  end
这个很好用。但是,如果我创建了一个新的任务订单,并且我将“responsibledamount”或“awardAmount”留空,那么Rails会将我带到带有错误片段的错误页面:

undefined method `>' for nil:NilClass'

def check_amount_obilgated #cannot be greater than contract award amount
    if obligatedAmount > awardAmount
        errors.add(:obligatedAmount, "The Obligated Amount cannot be greater than the Award Amount")
    end
  end

所以我想问题是,如果缺少一个或两个值,“>”操作符将无法工作。然而,我在validates中加入了:awardAmount和:responsibledamount。。。是否有任何方法可以让验证首先启动,或者有任何方法可以避免这个错误?请让我知道。谢谢

使用
to_i
将零转换为零

def check_amount_obilgated #cannot be greater than contract award amount
    if obligatedAmount.to_i > awardAmount.to_i
        errors.add(:obligatedAmount, "The Obligated Amount cannot be greater than the Award Amount")
    end
end

使用
to_i
将零转换为零

def check_amount_obilgated #cannot be greater than contract award amount
    if obligatedAmount.to_i > awardAmount.to_i
        errors.add(:obligatedAmount, "The Obligated Amount cannot be greater than the Award Amount")
    end
end

使用
to_i
将零转换为零

def check_amount_obilgated #cannot be greater than contract award amount
    if obligatedAmount.to_i > awardAmount.to_i
        errors.add(:obligatedAmount, "The Obligated Amount cannot be greater than the Award Amount")
    end
end

使用
to_i
将零转换为零

def check_amount_obilgated #cannot be greater than contract award amount
    if obligatedAmount.to_i > awardAmount.to_i
        errors.add(:obligatedAmount, "The Obligated Amount cannot be greater than the Award Amount")
    end
end

所以解释很简单。
操作符是在Fixnum类上定义的。NilClass没有定义
,因此它将抛出未定义的方法错误。如果将nil传递给有效调用,您将得到一个比较错误,因为nil不能被隐式强制为Fixnum

irb中的快速检查显示了如果在右操作数或左操作数中出现nil时可能出现的错误:

2.1.2 :001 > 1 > nil
ArgumentError: comparison of Fixnum with nil failed
    from (irb):1:in `>'
    from (irb):1
    from /Users/hungerandthirst/.rvm/rubies/ruby-2.1.2/bin/irb:11:in `<main>'
2.1.2 :002 > nil > 1
NoMethodError: undefined method `>' for nil:NilClass
    from (irb):2
    from /Users/hungerandthirst/.rvm/rubies/ruby-2.1.2/bin/irb:11:in `<main>'
因此,在代码中,请执行以下操作:

obligatedAmount.to_i > awardAmount.to_i

所以解释很简单。
操作符是在Fixnum类上定义的。NilClass没有定义
,因此它将抛出未定义的方法错误。如果将nil传递给有效调用,您将得到一个比较错误,因为nil不能被隐式强制为Fixnum

irb中的快速检查显示了如果在右操作数或左操作数中出现nil时可能出现的错误:

2.1.2 :001 > 1 > nil
ArgumentError: comparison of Fixnum with nil failed
    from (irb):1:in `>'
    from (irb):1
    from /Users/hungerandthirst/.rvm/rubies/ruby-2.1.2/bin/irb:11:in `<main>'
2.1.2 :002 > nil > 1
NoMethodError: undefined method `>' for nil:NilClass
    from (irb):2
    from /Users/hungerandthirst/.rvm/rubies/ruby-2.1.2/bin/irb:11:in `<main>'
因此,在代码中,请执行以下操作:

obligatedAmount.to_i > awardAmount.to_i

所以解释很简单。
操作符是在Fixnum类上定义的。NilClass没有定义
,因此它将抛出未定义的方法错误。如果将nil传递给有效调用,您将得到一个比较错误,因为nil不能被隐式强制为Fixnum

irb中的快速检查显示了如果在右操作数或左操作数中出现nil时可能出现的错误:

2.1.2 :001 > 1 > nil
ArgumentError: comparison of Fixnum with nil failed
    from (irb):1:in `>'
    from (irb):1
    from /Users/hungerandthirst/.rvm/rubies/ruby-2.1.2/bin/irb:11:in `<main>'
2.1.2 :002 > nil > 1
NoMethodError: undefined method `>' for nil:NilClass
    from (irb):2
    from /Users/hungerandthirst/.rvm/rubies/ruby-2.1.2/bin/irb:11:in `<main>'
因此,在代码中,请执行以下操作:

obligatedAmount.to_i > awardAmount.to_i

所以解释很简单。
操作符是在Fixnum类上定义的。NilClass没有定义
,因此它将抛出未定义的方法错误。如果将nil传递给有效调用,您将得到一个比较错误,因为nil不能被隐式强制为Fixnum

irb中的快速检查显示了如果在右操作数或左操作数中出现nil时可能出现的错误:

2.1.2 :001 > 1 > nil
ArgumentError: comparison of Fixnum with nil failed
    from (irb):1:in `>'
    from (irb):1
    from /Users/hungerandthirst/.rvm/rubies/ruby-2.1.2/bin/irb:11:in `<main>'
2.1.2 :002 > nil > 1
NoMethodError: undefined method `>' for nil:NilClass
    from (irb):2
    from /Users/hungerandthirst/.rvm/rubies/ruby-2.1.2/bin/irb:11:in `<main>'
因此,在代码中,请执行以下操作:

obligatedAmount.to_i > awardAmount.to_i
可能的重复可能的重复可能的重复可能的重复