有关在 Ryan bates Active_Merchant 集成视频上创建信用卡对象的问题
我正在观看 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 方法实际上是如何调用的。
I was going through Ryan Bates' video on Active Merchant integration video Railscast #145 and my question was regarding the creation of the @credit_card method that he defines within the Order.rb method.
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
)
end
What I don't follow is how does this method get called. The form_for in the new method creates an @order object while and there is no mention of the credit_card method. How does the credit_card method get called to initiate the creation of the @credit_card object.
I am aware of virtual attributes but I dont know how the credit_card method is actually called.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在此处查看截屏视频代码。
在app/views/orders/new.html.erb中,
我们可以看到订单表单,从第一行
我们可以看到,在提交时,表单使用了 oders_controller create 方法。
在app/controller/orders_controller.rb中
我们可以看到@order是从购物车构建的Order实例。这里没什么特别的
现在使用
@order.save
保存此订单,然后在 @order 上调用购买方法让我们来看看这个购买方法!
在app/model/order.rb
第二行
response = GATEWAY.purchase(price_in_cents,credit_card,purchase_options)
。 Credit_card 方法作为 GATEWAY.purchase 的参数被调用Looking at the screencast code here.
In app/views/orders/new.html.erb
We can see the order form, and from the first line
We see that, on submission, the form use the oders_controller create method.
In app/controller/orders_controller.rb
We can see that @order is Order instance built from the cart. Nothing special here
This order is now saved with
@order.save
and then the purchase method is called on @orderLet's take a look at this purchase method!
in app/model/order.rb
Second line
response = GATEWAY.purchase(price_in_cents, credit_card, purchase_options)
. The credit_card method is called there as an argument of GATEWAY.purchase