条件语句辅助和重构 Rails
Conditional statement assistance and refactoring Rails
我正在开发一个集成了比特币 API 的应用程序,我正在为条件语句而苦苦挣扎,我认为这是因为我对 ActiveRecord 或 SQL 方法缺乏了解。
这是期望的行为:
- 调用API并生成并保存一个比特币地址。
- 需要向用户显示相同的比特币地址,直到对其进行付款。
- 一旦付款完成并且用户想要进行另一笔付款,则需要再次调用 API 以生成新的比特币地址。
我找到了 find_or_create 方法,但我不确定如何在我的实现中使用它或使用哪种方法来表示 "If it does not exist - do this"
如果已经存在 Payment
,我目前的代码可以工作,但如果不存在 Payment
,我会收到无效或未知的 method/variable 错误。
If a Payment exists, and the last Payment's amount is 0.0 or nil, just show the last Payment's existing Bitcoin address.
if Payment.exists?
Payment.last.amount.to_f == 0.0 && Payment.last.amount == nil
@last_address = Payment.last.btc
@last_address
Then else if the Payment is greater than 0.0 generate and save a new Bitcoin address.
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 Just generate and save a new Bitcoin address.
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]
如有任何帮助或意见,我们将不胜感激。提前致谢。
编辑:当没有现有付款时,我收到以下错误:
NoMethodError - undefined method `amount' for nil:NilClass:
显示与 elsif 行相关:Payment.last.amount.to_f > 0.0
NoMethodError - undefined method
数量' nil:NilClass:
app/controllers/payments_controller.rb:17:in create'
actionpack (5.0.0.1) lib/action_controller/metal/basic_implicit_render.rb:4:in
send_action'
动作包 (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:in
process_action'
动作包 (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:in
call'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:126:in call'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:506:in
block (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:in
call'
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:in
process_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:in
block in process_action'
activesupport (5.0.0.1) lib/active_support/notifications.rb:164:in block in instrument'
我已经设法让代码在不存在付款的情况下工作,在我做的 if 块中:
if Payment.exists? && Payment.last.amount.to_f == 0.0 || Payment.exists? && Payment.last.amount == nil
而 elsif 块我只需要将 Payment.present?
更改为 Payment.exists?
这个方案对我来说还是很坑爹的,如果有更好的办法欢迎评论。
我正在开发一个集成了比特币 API 的应用程序,我正在为条件语句而苦苦挣扎,我认为这是因为我对 ActiveRecord 或 SQL 方法缺乏了解。
这是期望的行为:
- 调用API并生成并保存一个比特币地址。
- 需要向用户显示相同的比特币地址,直到对其进行付款。
- 一旦付款完成并且用户想要进行另一笔付款,则需要再次调用 API 以生成新的比特币地址。
我找到了 find_or_create 方法,但我不确定如何在我的实现中使用它或使用哪种方法来表示 "If it does not exist - do this"
如果已经存在 Payment
,我目前的代码可以工作,但如果不存在 Payment
,我会收到无效或未知的 method/variable 错误。
If a Payment exists, and the last Payment's amount is 0.0 or nil, just show the last Payment's existing Bitcoin address.
if Payment.exists?
Payment.last.amount.to_f == 0.0 && Payment.last.amount == nil
@last_address = Payment.last.btc
@last_address
Then else if the Payment is greater than 0.0 generate and save a new Bitcoin address.
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 Just generate and save a new Bitcoin address.
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]
如有任何帮助或意见,我们将不胜感激。提前致谢。
编辑:当没有现有付款时,我收到以下错误:
NoMethodError - undefined method `amount' for nil:NilClass:
显示与 elsif 行相关:Payment.last.amount.to_f > 0.0
NoMethodError - undefined method
数量' nil:NilClass:
app/controllers/payments_controller.rb:17:in create'
actionpack (5.0.0.1) lib/action_controller/metal/basic_implicit_render.rb:4:in
send_action'
动作包 (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:in
process_action'
动作包 (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:in
call'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:126:in call'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:506:in
block (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:in
call'
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:in
process_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:in
block in process_action'
activesupport (5.0.0.1) lib/active_support/notifications.rb:164:in block in instrument'
我已经设法让代码在不存在付款的情况下工作,在我做的 if 块中:
if Payment.exists? && Payment.last.amount.to_f == 0.0 || Payment.exists? && Payment.last.amount == nil
而 elsif 块我只需要将 Payment.present?
更改为 Payment.exists?
这个方案对我来说还是很坑爹的,如果有更好的办法欢迎评论。