Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.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 条件语句辅助和重构Rails_Ruby On Rails_Conditional_Ruby On Rails 5 - Fatal编程技术网

Ruby on rails 条件语句辅助和重构Rails

Ruby on rails 条件语句辅助和重构Rails,ruby-on-rails,conditional,ruby-on-rails-5,Ruby On Rails,Conditional,Ruby On Rails 5,我正在开发一款集成了比特币API的应用程序,我正在与条件语句作斗争,我认为这是因为我对ActiveRecord或SQL方法缺乏了解 这是理想的行为: 调用API并生成和保存比特币地址 在向用户付款之前,需要向用户显示相同的比特币地址 一旦支付了一笔款项,并且用户希望进行另一笔支付,则需要再次调用API以生成新的比特币地址 我找到了find_或create方法,但我不确定如何在我的实现中使用它,也不确定使用哪个方法来表示“如果它不存在,请执行此操作” 如果已经存在付款,则我迄今为止使用的代码有效,

我正在开发一款集成了比特币API的应用程序,我正在与条件语句作斗争,我认为这是因为我对ActiveRecord或SQL方法缺乏了解

这是理想的行为:

  • 调用API并生成和保存比特币地址
  • 在向用户付款之前,需要向用户显示相同的比特币地址
  • 一旦支付了一笔款项,并且用户希望进行另一笔支付,则需要再次调用API以生成新的比特币地址
  • 我找到了find_或create方法,但我不确定如何在我的实现中使用它,也不确定使用哪个方法来表示“如果它不存在,请执行此操作”

    如果已经存在
    付款
    ,则我迄今为止使用的代码有效,但如果没有现有的
    付款
    ,则我会得到无效或未知的方法/变量错误

    如果存在支付,且最后一次支付的金额为0.0或零,只需显示最后一次支付的现有比特币地址

    否则,如果付款大于0.0,则生成并保存新的比特币地址

    否则只需生成并保存一个新的比特币地址

    如何重构此代码,以及如何使用find_或create,或者如果记录不存在,如何通过调用API创建新记录

    用户不需要在视图中执行任何操作,因为一旦他们单击Pay,就会调用运行上述方法的页面。这是我的路线,效果很好:

    match '/save_btc' => 'payments#create', via: [:get, :post]
    
    如有任何帮助或意见,将不胜感激。提前谢谢

    编辑:当没有现有付款时,我得到以下错误:

    NoMethodError - undefined method `amount' for nil:NilClass:
    
    显示与elsif行相关:
    Payment.last.amount.to\u f>0.0

    NoMethodError-未定义的方法
    amount'表示零:零类: app/controllers/payments\u controller.rb:17:in
    create'
    actionpack(5.0.0.1)lib/action\u controller/metal/basic\u implicit\u render.rb:4:in
    send\u action' actionpack(5.0.0.1)lib/abstract\u controller/base.rb:188:in
    process\u action'
    actionpack(5.0.0.1)lib/action\u controller/metal/rendering.rb:30:in
    process\u action' actionpack(5.0.0.1)lib/abstract\u controller/callbacks.rb:20:in
    block in process\u action'
    activesupport(5.0.0.1)lib/active\u support/callbacks.rb:126:in
    call' activesupport(5.0.0.1)lib/active\u support/callbacks.rb:126:in
    call'
    activesupport(5.0.0.1)lib/active\u support/callbacks.rb:506:in
    block(2级)in compile' activesupport(5.0.0.1)lib/active\u support/callbacks.rb:455:in
    call'
    activesupport(5.0.0.1)lib/active\u support/callbacks.rb:455:in
    call' activesupport(5.0.0.1)lib/active\u support/callbacks.rb:101:in
    \uuuu run\u callbacks\uuuuu'
    activesupport(5.0.0.1)lib/active\u support/callbacks.rb:750:in
    \u run\u process\u action\u callbacks' activesupport(5.0.0.1)lib/active\u support/callbacks.rb:90:in
    run\u callbacks'
    actionpack(5.0.0.1)lib/abstract\u controller/callbacks.rb:19:in
    process\u action' actionpack(5.0.0.1)lib/action\u controller/metal/rescue.rb:20:in
    process\u action'
    actionpack(5.0.0.1)lib/action\u controller/metal/instrumentation.rb:32:in
    block in process\u action'
    activesupport(5.0.0.1)lib/active\u support/notifications.rb:164:in
    instrument中的block'

    我已设法使代码在不存在付款时工作,在if block中我做到了:

    if Payment.exists? && Payment.last.amount.to_f == 0.0 || Payment.exists? && Payment.last.amount == nil
    
    而elsif块我只需将
    Payment.present?
    更改为
    Payment.exists?


    这个解决方案仍然让我感觉很不舒服,请评论是否有更好的方法。

    我已经设法让代码在不存在付款的情况下工作,在if块中我做到了:

    if Payment.exists? && Payment.last.amount.to_f == 0.0 || Payment.exists? && Payment.last.amount == nil
    
    而elsif块我只需将
    Payment.present?
    更改为
    Payment.exists?


    这个解决方案仍然让我感觉很不舒服,如果有更好的方法,请评论。

    你能发布你在问题中遇到的错误的堆栈跟踪吗?@CdotStrifeVII我已经在问题中添加了关于错误的其他信息,谢谢。你能发布你在问题中得到的错误的堆栈跟踪吗?@CdotStrifeVII我已经在问题中添加了关于错误的附加信息,谢谢。
    NoMethodError - undefined method `amount' for nil:NilClass:
    
    if Payment.exists? && Payment.last.amount.to_f == 0.0 || Payment.exists? && Payment.last.amount == nil