繁体   English   中英

如何将内联Ruby每个循环放入外部请求中?

[英]How to put inline Ruby each loop in an external request?

我使用Rails 3.1和paypal_adaptive( https://github.com/tc/paypal_adaptive )gem来向Paypal发出API请求。 这是交易:我有一个正在控制器中向Paypal发出的请求。 我有一个Items集合(这里是@items),并且我正在尝试为该集合中的每个项目将收件人添加到该请求中。 这是我的代码:

pay_request = PaypalAdaptive::Request.new

data = {
  "returnUrl" => "http://testserver.com/payments/completed_payment_request",
  "requestEnvelope" => {"errorLanguage" => "en_US"},
  "currencyCode" => "USD",
  "receiverList" => {"receiver"=>[
    @items.each do |item|
      {"email"=>"#{Recipient.find_by_id(item.recip_id).email}", "amount"=>"#{item.price}"},
    end
  ]},
  "cancelUrl"=>"http://testserver.com/payments/canceled_payment_request",
  "actionType"=>"PAY",
  "ipnNotificationUrl"=>"http://testserver.com/payments/ipn_notification"
}

pay_response = pay_request.pay(data)

if pay_response.success?
  redirect_to pay_response.approve_paypal_payment_url
else
  puts pay_response.errors.first['message']
  redirect_to failed_payment_url
end

...但这是行不通的。 如何将Ruby语法正确插入此请求中?

(很抱歉标题中的含糊之处。我不确定我要发出什么样的请求(也许是AJAX?)

只需将所有项目收集到一个变量中并将其设置为“ reciever”键的值即可:

recipients = @items.each do |item|
  {"email"=>"#{Recipient.find_by_id(item.recip_id).email}", "amount"=>"#{item.price}"}
end

...

"currencyCode"=>"USD",  
"receiverList"=>{"receiver"=> recipients},
"cancelUrl"=>"http://testserver.com/payments/canceled_payment_request",

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM