【问题标题】:Conditional statement assistance and refactoring Rails条件语句辅助和重构 Rails
【发布时间】:2017-03-18 01:25:29
【问题描述】:

我正在开发一个集成了比特币 API 的应用程序,我正在为条件语句而苦苦挣扎,我认为这是由于我对 ActiveRecord 或 SQL 方法缺乏了解。

这是期望的行为:

  1. 调用 API 并生成并保存比特币地址。
  2. 在向用户付款之前,需要向用户显示相同的比特币地址。
  3. 一旦完成支付,并且用户想要进行另一次支付,则需要再次调用 API 以生成新的比特币地址。

我找到了 find_or_create 方法,但我不确定如何在我的实现中使用它或使用哪种方法来表示“如果它不存在 - 执行此操作”

如果已经存在Payment,我到目前为止的代码可以工作,但如果不存在Payment,我会收到无效或未知的方法/变量错误。

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

if Payment.exists?
 Payment.last.amount.to_f == 0.0 && Payment.last.amount == nil
 @last_address = Payment.last.btc
 @last_address

然后,如果付款大于 0.0,则生成并保存一个新的比特币地址。

elsif Payment.present?
 Payment.last.amount.to_f > 0.0
 @new_address = BlockIo.get_new_address
 Payment.create( btc: @new_address['data']['address'] )
  respond_to do |format|
   format.html { redirect_to '/save_btc'}
  end

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

else
@new_address = BlockIo.get_new_address
Payment.create( btc: @new_address['data']['address'] )
 respond_to do |format|
  format.html { redirect_to '/save_btc'}
 end
end

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

用户不需要在视图中做任何事情,因为一旦他们点击支付,运行上述方法的页面就会被调用。这是我的路线,效果很好:

match '/save_btc' => 'payments#create', via: [:get, :post]

任何帮助或 cmets 将不胜感激。提前致谢。

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

NoMethodError - undefined method `amount' for nil:NilClass:

这表明与 elsif 行相关:Payment.last.amount.to_f > 0.0

NoMethodError - undefined methodamount' 对于 nil:NilClass: 应用程序/控制器/payments_controller.rb:17:in create' actionpack (5.0.0.1) lib/action_controller/metal/basic_implicit_render.rb:4:insend_action' actionpack (5.0.0.1) lib/abstract_controller/base.rb:188:in process_action' actionpack (5.0.0.1) lib/action_controller/metal/rendering.rb:30:inprocess_action' actionpack (5.0.0.1) lib/abstract_controller/callbacks.rb:20:in block in process_action' activesupport (5.0.0.1) lib/active_support/callbacks.rb:126:incall' activesupport (5.0.0.1) lib/active_support/callbacks.rb:126:in call' activesupport (5.0.0.1) lib/active_support/callbacks.rb:506:inblock (2 levels) in compile' activesupport (5.0.0.1) lib/active_support/callbacks.rb:455:in call' activesupport (5.0.0.1) lib/active_support/callbacks.rb:455:incall' activesupport (5.0.0.1) lib/active_support/callbacks.rb:101:in __run_callbacks__' activesupport (5.0.0.1) lib/active_support/callbacks.rb:750:in_run_process_action_callbacks' activesupport (5.0.0.1) lib/active_support/callbacks.rb:90:in run_callbacks' actionpack (5.0.0.1) lib/abstract_controller/callbacks.rb:19:inprocess_action' actionpack (5.0.0.1) lib/action_controller/metal/rescue.rb:20:in process_action' actionpack (5.0.0.1) lib/action_controller/metal/instrumentation.rb:32:inblock in process_action' activesupport (5.0.0.1) lib/active_support/notifications.rb:164:in block in instrument'

【问题讨论】:

  • 您可以发布您在问题中遇到的错误的堆栈跟踪吗?
  • @CdotStrifeVII 我已在问题中添加了有关错误的其他信息,谢谢。

标签: ruby-on-rails conditional ruby-on-rails-5


【解决方案1】:

在我所做的 if 块中,当没有付款存在时,我已经设法让代码工作:

if Payment.exists? && Payment.last.amount.to_f == 0.0 || Payment.exists? && Payment.last.amount == nil

而我只需要将 Payment.present? 更改为 Payment.exists? 的 elsif 块

这个解决方案对我来说仍然很hacky,如果有更好的方法,请发表评论。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-09
    • 2016-06-25
    • 1970-01-01
    • 1970-01-01
    • 2018-05-18
    • 1970-01-01
    相关资源
    最近更新 更多