简体   繁体   English

params[…] 比较在 ruby​​ on rails 中引发了“undefined method `>' for nil:NilClass”错误

[英]params[…] comparison raises “undefined method `>' for nil:NilClass” error in ruby on rails

I want to check below:我想在下面检查:

if params[:hidval] > "0"
OR
if !params[:hidval] < "1"

But it gave me error below:但它给了我以下错误:

undefined method `>' for nil:NilClass

How do I check above conditions in ruby on rails?如何在 ruby​​ on rails 中检查上述条件?

The error message says it all.错误消息说明了一切。 Make sure, the param actually exists.确保参数确实存在。 You try to compare nothing ( NilClass ) with a number.您尝试将任何内容( NilClass )与数字进行比较。 (which is actually a string, that will be your next problem) (这实际上是一个字符串,这将是您的下一个问题)

Probably correct would be:可能正确的是:

if params[:hidval].present? && params[:hidval] > 0
 # Do something ...
end

As of Ruby 2.3, you can use the Safe Navigation Operator &.从 Ruby 2.3 开始,您可以使用安全导航运算符&. to streamline this:简化这个:

irb(main):028:0>   5 > 0
=> true
irb(main):029:0> nil > 0
NoMethodError: undefined method '>' for nil:NilClass
    from (irb):29
irb(main):030:0>   5 &.> 0
=> true
irb(main):031:0> nil &.> 0
=> nil
irb(main):032:0>   5 &.> 99
=> false

This will typically get you what you want (eg conditional branching) since nil is falsey.这通常会得到你想要的(例如条件分支),因为nil是假的。 But keep that in mind if you actually depend on an exact false value.但是如果你真的依赖一个精确的false值,请记住这一点。

&. means to only call the trailing method if the leading object is not null.表示仅在前导对象不为空时才调用尾随方法。 It makes a lot more sense in a chained method call like:它在链式方法调用中更有意义,例如:

user&.deactivate!

But, since the greater-than operator > is actually a method in Ruby, it works here as well.但是,由于大于号运算符>实际上是Ruby 中的一个方法,因此它也可以在这里使用。

Personally, I'm not convinced the painful aesthetics of params[:hidval] &.> "0" makes this worthwhile...but that's just a question of style.就我个人而言,我不相信params[:hidval] &.> "0"的痛苦美学使这变得值得......但这只是一个风格问题。

Try this code试试这个代码

if params[:hidval].present?
  if params[:hidval].to_i > 0
    # Greater than 0 condition
  else
    # Less than equal to 0 condition
  end
end

Re-reading this question, I realize I missed the mark on my first answer.重新阅读这个问题,我意识到我错过了第一个答案的标记。 It side-stepped the problem without addressing it.它回避了这个问题而没有解决它。

The real problem you saw this error is that you probably put !你看到这个错误的真正问题是你可能把! in the wrong place.在错误的地方。

If you meant, if it is not less than "1" , then you should write:如果你的意思是, if it is not less than "1" ,那么你应该写:

if !( params[:hidval] < "1" )

This compares hidval and "1" and THEN negates the result.这将 hidval 和 "1" 进行比较,然后否定结果。

What you posted:您发布的内容:

if !params[:hidval] < "1"

...first negates hidval (usually resulting in false ) and then compares false (or possibly true ) to see if it is less than "1". ...首先否定 hidval (通常导致false ),然后比较false (或可能是true )以查看它是否小于“1”。 Not only does comparing false to a string not make much sense, you'll actually get you this error:不仅将false与字符串进行比较没有多大意义,您实际上还会收到此错误:

NoMethodError (undefined method `<' for false:FalseClass)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Ruby on Rails:params是零。 未定义的方法`[]&#39;为nil:NilClass - Ruby on Rails: params is nil. undefined method `[]' for nil:NilClass Ruby on Rails错误:nil:NilClass的未定义方法 - Ruby on Rails error: undefined method for nil:NilClass 错误未定义方法 `+' for nil:NilClass // Ruby on Rails - Error undefined method `+' for nil:NilClass // Ruby on Rails 在Ruby on Rails中更改if结构中的参数会导致错误(对于nil:NilClass,未定义方法&#39;[]&#39;) - Changing params in an if structure causes error (undefined method '[]' for nil:NilClass) in Ruby on Rails 带有rails params的nil:NilClass的未定义方法&#39;[]&#39; - undefined method `[]' for nil:NilClass with rails params Ruby on Rails中“ nil:NilClass的未定义方法&#39;title&#39;”错误 - “undefined method `title' for nil:NilClass” error in Ruby on Rails Ruby on Rails - nil:NilClass 错误的未定义方法`each&#39; - Ruby on Rails - undefined method `each' for nil:NilClass error nil:NilClass ruby​​在rails上的未定义方法`downcase&#39; - undefined method `downcase' for nil:NilClass ruby on rails Ruby on Rails-nil:NilClass的未定义方法“错误” - Ruby on Rails - undefined method `errors' for nil:NilClass Ruby on Rails:nil:NilClass的未定义方法“ instructors” - Ruby on Rails : undefined method `instructors' for nil:NilClass
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM