gpt4 book ai didi

ruby-on-rails - 关于在 Ryan bates Active_Merchant 集成视频中创建信用卡对象的问题

转载 作者:太空宇宙 更新时间:2023-11-03 16:38:48 24 4
gpt4 key购买 nike

我正在观看 Ryan Bates 关于 Active Merchant 集成视频 Railscast #145 的视频,我的问题是关于他在 Order.rb 方法中定义的 @credit_card 方法的创建。

def credit_card
@credit_card||=ActiveMerchant::Billing::CreditCard.new(
:type=>card_type,
:number=>card_number,
:verification_value=>card_verification,
:month=>card_expires_on.month,
:year=>card_expires_on.year,
:first_name=>first_name,
:last_name=>last_name
)

结束

我不了解的是如何调用此方法。新方法中的 form_for 创建了一个 @order 对象,而没有提及 credit_card 方法。如何调用 credit_card 方法来启动 @credit_card 对象的创建。

我知道虚拟属性,但我不知道 credit_card 方法实际上是如何调用的。

最佳答案

查看截屏代码 here .

在 app/views/orders/new.html.erb

我们可以看到订单,从第一行开始

<% form_for @order do |f| %>

我们看到,在提交时,表单使用 oders_controller 创建方法。

在 app/controller/orders_controller.rb

    def create
@order = current_cart.build_order(params[:order])
@order.ip_address = request.remote_ip

if @order.save
if @order.purchase
render :action => "success"
else
render :action => "failure"
end
else
render :action => 'new'
end
end

我们可以看到 @order 是从购物车构建的 Order 实例。这里没什么特别的此订单现在使用 @order.save 保存,然后在 @order 上调用购买方法

一起来看看这个购买方式吧!

在 app/model/order.rb 中

    def purchase
response = GATEWAY.purchase(price_in_cents, credit_card, purchase_options)
transactions.create!(:action => "purchase", :amount => price_in_cents, :response => response)
cart.update_attribute(:purchased_at, Time.now) if response.success?
response.success?
end

第二行 response = GATEWAY.purchase(price_in_cents, credit_card, purchase_options)
。 credit_card 方法作为 GATEWAY.purchase 的参数被调用

关于ruby-on-rails - 关于在 Ryan bates Active_Merchant 集成视频中创建信用卡对象的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1697500/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com