NoMethodError(nil:NilClass 的未定义方法“<”):
NoMethodError (undefined method `<' for nil:NilClass):
这是我收到的错误:
NoMethodError (undefined method `<' for nil:NilClass):
app/controllers/concerns/withdraws/withdrawable.rb:20:in `create'
这是有问题的代码部分:
def create
@withdraw = model_kls.new(withdraw_params)
@verified = current_user.id_document_verified?
@local_sum = params[:withdraw][:sum]
if !@local_sum
render text: I18n.t('private.withdraws.create.amount_empty_error'), status: 403
return
end
if !@verified && channel.currency_obj.withdraw_limit < @local_sum <<<<<- Here is the error
render text: I18n.t('private.withdraws.create.unverified_withdraw_limit_error', limit: channel.currency_obj.withdraw_limit), status: 403
return
end
这就是我的全部代码:
https://github.com/DigitalCoin1/Spero-Exchange
有问题的错误在这个文件中:
非常感谢!!!
当channel.currency_obj.withdraw_limit
return nil
或 @local_sum
为nil时发生错误。
它无法比较 nil
值。
您必须再次检查 @local_sum
,并确保它有值。或者 channel.currency_obj.withdraw_limit
确保它有一个值。
但我猜 channel.currency_obj.withdraw_limit
return nil
.
那是你的问题。
NoMethodError (undefined method `<' for nil:NilClass):
app/controllers/concerns/withdraws/withdrawable.rb:20:in `create'
此错误表示它正在尝试将 <
与 nil
值进行比较。
能否请您print
检查错误语句前的channel.currency_obj.withdraw_limit
和@local_sum
。
为避免 nil
错误,您可以包含 nil check
.
if channel.currency_obj.withdraw_limit != nil and @local_sum != nil
记住,(almost) everything is Ruby is an object...包括nil
。
牢记这一点,考虑当您调用 nil
:
上不存在的方法时会发生什么
irb(main):001:0> nil.something
Traceback (most recent call last):
2: from /Users/scott/.rbenv/versions/2.5.1/bin/irb:11:in `<main>'
1: from (irb):1
NoMethodError (undefined method `something' for nil:NilClass)
此外,在Ruby中,>
、<
、==
等运算符实际上是方法调用。因此,例如,Integer
的实例(例如 3
)在其上定义了一个名为 <
的方法,当您调用 3 < 4
时,它会调用该实例上的方法。之所以这样工作,是因为在 Ruby 中,您可以在进行方法调用时省略括号。例如:
irb(main):001:0> 3 < 4
=> true
irb(main):002:0> 3.<(4)
=> true
所以把这两个例子放在一起:
irb(main):014:0> nil < 4
Traceback (most recent call last):
2: from /Users/scott/.rbenv/versions/2.5.1/bin/irb:11:in `<main>'
1: from (irb):14
NoMethodError (undefined method `<' for nil:NilClass)
现在,让我们看一下您的代码。
你遇到了异常:
NoMethodError (undefined method `<' for nil:NilClass)
这一行:
!@verified && channel.currency_obj.withdraw_limit < @local_sum
查看这段代码,您只在一个地方调用了 <
。这意味着它左边的任何东西 (channel.currency_obj.withdraw_limit
) 都必须是 nil
.
有几种方法可以解决这个问题...最好的方法(在我看来)是确保 channel.currency_obj
永远不会是 nil
。不幸的是,我没有足够的代码来向您确切地展示如何做到这一点,所以让我们看看其他一些选择...
我们可以使用 Ruby 2.3+'s safe navigation operator (&.
) -- 但是与 <
.
这样的运算符一起使用有点奇怪
channel.currency_obj.withdraw_limit&. < @local_sum
注意:在此示例中,表达式的计算结果为 nil
,并且由于 nil
为假,因此条件将 return 为假。
或者,我们可以在条件语句中添加另一个表达式来检查 nil
:
!@verified && channel.currency_obj.withdraw_limit && channel.currency_obj.withdraw_limit < @local_sum
这是我收到的错误:
NoMethodError (undefined method `<' for nil:NilClass):
app/controllers/concerns/withdraws/withdrawable.rb:20:in `create'
这是有问题的代码部分:
def create
@withdraw = model_kls.new(withdraw_params)
@verified = current_user.id_document_verified?
@local_sum = params[:withdraw][:sum]
if !@local_sum
render text: I18n.t('private.withdraws.create.amount_empty_error'), status: 403
return
end
if !@verified && channel.currency_obj.withdraw_limit < @local_sum <<<<<- Here is the error
render text: I18n.t('private.withdraws.create.unverified_withdraw_limit_error', limit: channel.currency_obj.withdraw_limit), status: 403
return
end
这就是我的全部代码:
https://github.com/DigitalCoin1/Spero-Exchange
有问题的错误在这个文件中:
非常感谢!!!
当channel.currency_obj.withdraw_limit
return nil
或 @local_sum
为nil时发生错误。
它无法比较 nil
值。
您必须再次检查 @local_sum
,并确保它有值。或者 channel.currency_obj.withdraw_limit
确保它有一个值。
但我猜 channel.currency_obj.withdraw_limit
return nil
.
那是你的问题。
NoMethodError (undefined method `<' for nil:NilClass):
app/controllers/concerns/withdraws/withdrawable.rb:20:in `create'
此错误表示它正在尝试将 <
与 nil
值进行比较。
能否请您print
检查错误语句前的channel.currency_obj.withdraw_limit
和@local_sum
。
为避免 nil
错误,您可以包含 nil check
.
if channel.currency_obj.withdraw_limit != nil and @local_sum != nil
记住,(almost) everything is Ruby is an object...包括nil
。
牢记这一点,考虑当您调用 nil
:
irb(main):001:0> nil.something
Traceback (most recent call last):
2: from /Users/scott/.rbenv/versions/2.5.1/bin/irb:11:in `<main>'
1: from (irb):1
NoMethodError (undefined method `something' for nil:NilClass)
此外,在Ruby中,>
、<
、==
等运算符实际上是方法调用。因此,例如,Integer
的实例(例如 3
)在其上定义了一个名为 <
的方法,当您调用 3 < 4
时,它会调用该实例上的方法。之所以这样工作,是因为在 Ruby 中,您可以在进行方法调用时省略括号。例如:
irb(main):001:0> 3 < 4
=> true
irb(main):002:0> 3.<(4)
=> true
所以把这两个例子放在一起:
irb(main):014:0> nil < 4
Traceback (most recent call last):
2: from /Users/scott/.rbenv/versions/2.5.1/bin/irb:11:in `<main>'
1: from (irb):14
NoMethodError (undefined method `<' for nil:NilClass)
现在,让我们看一下您的代码。
你遇到了异常:
NoMethodError (undefined method `<' for nil:NilClass)
这一行:
!@verified && channel.currency_obj.withdraw_limit < @local_sum
查看这段代码,您只在一个地方调用了 <
。这意味着它左边的任何东西 (channel.currency_obj.withdraw_limit
) 都必须是 nil
.
有几种方法可以解决这个问题...最好的方法(在我看来)是确保 channel.currency_obj
永远不会是 nil
。不幸的是,我没有足够的代码来向您确切地展示如何做到这一点,所以让我们看看其他一些选择...
我们可以使用 Ruby 2.3+'s safe navigation operator (&.
) -- 但是与 <
.
channel.currency_obj.withdraw_limit&. < @local_sum
注意:在此示例中,表达式的计算结果为 nil
,并且由于 nil
为假,因此条件将 return 为假。
或者,我们可以在条件语句中添加另一个表达式来检查 nil
:
!@verified && channel.currency_obj.withdraw_limit && channel.currency_obj.withdraw_limit < @local_sum