【问题标题】:Rails loop through inserted variable from database to url for external callRails 循环插入变量从数据库到 url 以进行外部调用
【发布时间】:2017-07-10 01:10:34
【问题描述】:

我正在尝试将来自 Solidus 的订单的 line_items 显示到我的 rails 应用程序的 Items 表中。以前我将来自 Solidus 的订单保存到我的“订单”(在我的应用程序中)表中,属性为“order_number”

我需要将 order_number 插入到我用于使用 httparty 进行外部调用的 url 中,并遍历 order_number 中的所有行以在我的索引视图中显示每个订单的属性。每个订单,例如:http://localhost:4000/api/orders/R984044507.json?token=fd5a8b4b294303ad52fcb19a9eaf8bb7325d20a8aa5abbc7 有这样的结构:

 {
"id": 3,
"number": "R984044507",
"item_total": "59.97",
"total": "68.22",
"ship_total": "5.0",
"state": "complete",
"adjustment_total": "3.25",
"user_id": null,
"created_at": "2017-07-09T17:12:19.209Z",
"updated_at": "2017-07-09T17:14:24.657Z",
"completed_at": "2017-07-09T17:14:24.657Z",
"payment_total": "0.0",
"shipment_state": "pending",
"payment_state": "balance_due",
"email": "ignaciosm@gmail.com",
"special_instructions": null,
"channel": "spree",
"included_tax_total": "0.0",
"additional_tax_total": "3.25",
"display_included_tax_total": "$0.00",
"display_additional_tax_total": "$3.25",
"tax_total": "3.25",
"currency": "USD",
"covered_by_store_credit": false,
"display_total_applicable_store_credit": "$0.00",
"order_total_after_store_credit": "68.22",
"display_order_total_after_store_credit": "$68.22",
"total_applicable_store_credit": "0.0",
"display_total_available_store_credit": "$0.00",
"display_store_credit_remaining_after_capture": "$0.00",
"canceler_id": null,
"display_item_total": "$59.97",
"total_quantity": 3,
"display_total": "$68.22",
"display_ship_total": "$5.00",
"display_tax_total": "$3.25",
"token": "vQyNyoZEdjOux5XYOCdafg",
"checkout_steps": [],
"payment_methods": [],
"bill_address": {},
"ship_address": {},
"line_items": [
{
"id": 3,
"quantity": 1,
"price": "19.99",
"variant_id": 17,
"variant": {
"product_id": 3,
"id": 17,
"name": "Ruby on Rails Baseball Jersey",
"sku": "ROR-00008",
"price": "19.99",
"weight": "0.0",
"height": null,
"width": null,
"depth": null,
"is_master": false,
"slug": "ruby-on-rails-baseball-jersey",
"description": "Fuga quia cumque sunt dignissimos eum unde dicta doloribus. Iste sit inventore doloribus et aut eum totam nisi. Ut quas explicabo omnis eos ipsa consequuntur. Aut a quis molestiae reprehenderit suscipit expedita.",
"track_inventory": true,
"cost_price": "17.0",
"option_values": [
{
"id": 3,
"name": "Large",
"presentation": "L",
"option_type_name": "tshirt-size",
"option_type_id": 1,
"option_type_presentation": "Size"
},
{
"id": 7,
"name": "Blue",
"presentation": "Blue",
"option_type_name": "tshirt-color",
"option_type_id": 2,
"option_type_presentation": "Color"
}
],
"images": [
{
"id": 15,
"position": 1,
"attachment_content_type": "image/png",
"attachment_file_name": "ror_baseball_jersey_blue.png",
"type": "Spree::Image",
"attachment_updated_at": "2017-07-08T23:10:54.595Z",
"attachment_width": 240,
"attachment_height": 240,
"alt": null,
"viewable_type": "Spree::Variant",
"viewable_id": 17,
"mini_url": "/spree/products/15/mini/ror_baseball_jersey_blue.png?1499555454",
"small_url": "/spree/products/15/small/ror_baseball_jersey_blue.png?1499555454",
"product_url": "/spree/products/15/product/ror_baseball_jersey_blue.png?1499555454",
"large_url": "/spree/products/15/large/ror_baseball_jersey_blue.png?1499555454"
},
{
"id": 16,
"position": 2,
"attachment_content_type": "image/png",
"attachment_file_name": "ror_baseball_jersey_back_blue.png",
"type": "Spree::Image",
"attachment_updated_at": "2017-07-08T23:10:55.123Z",
"attachment_width": 240,
"attachment_height": 240,
"alt": null,
"viewable_type": "Spree::Variant",
"viewable_id": 17,
"mini_url": "/spree/products/16/mini/ror_baseball_jersey_back_blue.png?1499555455",
"small_url": "/spree/products/16/small/ror_baseball_jersey_back_blue.png?1499555455",
"product_url": "/spree/products/16/product/ror_baseball_jersey_back_blue.png?1499555455",
"large_url": "/spree/products/16/large/ror_baseball_jersey_back_blue.png?1499555455"
}
],
"display_price": "$19.99",
"options_text": "Size: L, Color: Blue",
"in_stock": true,
"is_backorderable": true,
"total_on_hand": 9,
"is_destroyed": false
},
"adjustments": [
{
"id": 5,
"source_type": "Spree::TaxRate",
"source_id": 1,
"adjustable_type": "Spree::LineItem",
"adjustable_id": 3,
"amount": "1.0",
"label": "North America 5.000%",
"promotion_code": null,
"eligible": true,
"created_at": "2017-07-09T17:13:56.297Z",
"updated_at": "2017-07-09T17:14:24.280Z",
"display_amount": "$1.00"
}
],
"single_display_amount": "$19.99",
"display_amount": "$19.99",
"total": "20.99"
},
{
"id": 4,
"quantity": 1,
"price": "19.99",
"variant_id": 6,
"variant": {
"product_id": 6,
"id": 6,
"name": "Ruby Baseball Jersey",
"sku": "RUB-00001",
"price": "19.99",
"weight": "0.0",
"height": null,
"width": null,
"depth": null,
"is_master": true,
"slug": "ruby-baseball-jersey",
"description": "Fuga quia cumque sunt dignissimos eum unde dicta doloribus. Iste sit inventore doloribus et aut eum totam nisi. Ut quas explicabo omnis eos ipsa consequuntur. Aut a quis molestiae reprehenderit suscipit expedita.",
"track_inventory": true,
"cost_price": "17.0",
"option_values": [],
"images": [
{
"id": 34,
"position": 1,
"attachment_content_type": "image/png",
"attachment_file_name": "ruby_baseball.png",
"type": "Spree::Image",
"attachment_updated_at": "2017-07-08T23:11:04.206Z",
"attachment_width": 495,
"attachment_height": 477,
"alt": null,
"viewable_type": "Spree::Variant",
"viewable_id": 6,
"mini_url": "/spree/products/34/mini/ruby_baseball.png?1499555464",
"small_url": "/spree/products/34/small/ruby_baseball.png?1499555464",
"product_url": "/spree/products/34/product/ruby_baseball.png?1499555464",
"large_url": "/spree/products/34/large/ruby_baseball.png?1499555464"
}
],
"display_price": "$19.99",
"options_text": "",
"in_stock": true,
"is_backorderable": true,
"total_on_hand": 9,
"is_destroyed": false
},
"adjustments": [
{
"id": 6,
"source_type": "Spree::TaxRate",
"source_id": 1,
"adjustable_type": "Spree::LineItem",
"adjustable_id": 4,
"amount": "1.0",
"label": "North America 5.000%",
"promotion_code": null,
"eligible": true,
"created_at": "2017-07-09T17:13:56.327Z",
"updated_at": "2017-07-09T17:14:24.300Z",
"display_amount": "$1.00"
}
],
"single_display_amount": "$19.99",
"display_amount": "$19.99",
"total": "20.99"
},
{
"id": 5,
"quantity": 1,
"price": "19.99",
"variant_id": 7,
"variant": {
"product_id": 7,
"id": 7,
"name": "Apache Baseball Jersey",
"sku": "APC-00001",
"price": "19.99",
"weight": "0.0",
"height": null,
"width": null,
"depth": null,
"is_master": true,
"slug": "apache-baseball-jersey",
"description": "Fuga quia cumque sunt dignissimos eum unde dicta doloribus. Iste sit inventore doloribus et aut eum totam nisi. Ut quas explicabo omnis eos ipsa consequuntur. Aut a quis molestiae reprehenderit suscipit expedita.",
"track_inventory": true,
"cost_price": "17.0",
"option_values": [],
"images": [
{
"id": 33,
"position": 1,
"attachment_content_type": "image/png",
"attachment_file_name": "apache_baseball.png",
"type": "Spree::Image",
"attachment_updated_at": "2017-07-08T23:11:03.616Z",
"attachment_width": 504,
"attachment_height": 484,
"alt": null,
"viewable_type": "Spree::Variant",
"viewable_id": 7,
"mini_url": "/spree/products/33/mini/apache_baseball.png?1499555463",
"small_url": "/spree/products/33/small/apache_baseball.png?1499555463",
"product_url": "/spree/products/33/product/apache_baseball.png?1499555463",
"large_url": "/spree/products/33/large/apache_baseball.png?1499555463"
}
],
"display_price": "$19.99",
"options_text": "",
"in_stock": true,
"is_backorderable": true,
"total_on_hand": 9,
"is_destroyed": false
},
"adjustments": [
{
"id": 7,
"source_type": "Spree::TaxRate",
"source_id": 1,
"adjustable_type": "Spree::LineItem",
"adjustable_id": 5,
"amount": "1.0",
"label": "North America 5.000%",
"promotion_code": null,
"eligible": true,
"created_at": "2017-07-09T17:13:56.337Z",
"updated_at": "2017-07-09T17:14:24.313Z",
"display_amount": "$1.00"
}
],
"single_display_amount": "$19.99",
"display_amount": "$19.99",
"total": "20.99"
}
]
}

这是我的控制器:

# items controller      
     def index
          order_number = 'R984044507' # just to test until I figure out how to loop through order_ids
          url = 'http://localhost:4000/api/orders/'
          key = '.json?token=fd5a8b4b294303ad52fcb19a9eaf8bb7325d20a8aa5abbc7'
          response = HTTParty.get(url+order_number+key)

          @items = response.parsed_response    
      end

我的索引如下所示:

<table>
  <thead>
    <tr>
      <th>Order</th>
      <th>Variand_Id</th>
      <th>Product</th>
      <th>Price</th>
      <th>Qty</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>

    <% (@items['line_items']).each do |item| %>
      <tr>
        <td><%= @items['number'] %></td>
        <td><%= item['variant']["id"] %></td>
        <td><%= item['variant']["name"] %></td>
        <td><%= item['variant']['price'] %></td>
        <td><%= item["quantity"] %></td>

      </tr>
    <% end %>
  </tbody>

</table>

我在我的 Orders 表中对其中一个订单的订单号进行了硬编码,以测试它是否显示了每个订单中的商品,但我不知道如何遍历订单,因此我从每个订单中获取商品。

我不确定我的方法是否最好,我是初学者。任何帮助将不胜感激。

【问题讨论】:

  • 您要循环哪些订单 ID?他们都是?您需要先获取 id,然后才能循环它们(可能使用另一个 API 端点)。
  • 是的,我已经得到它们并将它们保存到我的订单表(Solidus 中的所有待处理订单)......所以对于我的问题,假设我想要查看的订单是我的订单上的所有订单表

标签: ruby-on-rails json spree httparty solidus


【解决方案1】:

你可以这样做:

控制器

def index
  orders = Order.all.pluck(:number)

  @items = []
  orders.each do |order|
    order_number = order
    url = 'http://localhost:4000/api/orders/'
    key = '.json?token=fd5a8b4b294303ad52fcb19a9eaf8bb7325d20a8aa5abbc7'
    response = HTTParty.get(url+order_number+key)

    @items << response.parsed_response
  end
end

首先您获得所有订单号(我假设numberorders 表中的列名),然后您遍历它们,请求每个订单并将其属性添加到@items 数组。

查看

<table>
  <thead>
    <tr>
      <th>Order</th>
      <th>Variand_Id</th>
      <th>Product</th>
      <th>Price</th>
      <th>Qty</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @items.each do |item| %>
      <% item['line_items'].each do |line_item| %>
        <tr>
          <td><%= item['number'] %></td>
          <td><%= line_item['variant']["id"] %></td>
          <td><%= line_item['variant']["name"] %></td>
          <td><%= line_item['variant']['price'] %></td>
          <td><%= line_item["quantity"] %></td>
        </tr>
      <% end %>
    <% end %>
  </tbody>
</table>

由于@items 是一个数组,您也需要对其进行迭代,因此需要两个循环:一个用于@items,一个用于line_items

虽然此解决方案应该可行,但请注意,请求大量订单可能会导致性能问题。

【讨论】:

  • 谢谢 Gerry,我不太明白 order_number 是如何在 url 中循环的……或者假设我们有 3 个订单:R984044507、R601754941 和 R036622716。要从每个订单中获取 line_items,应该调用包含每个订单的 URL。示例:1)localhost:4000/api/orders/R984044507.json?token=... 2)localhost:4000/api/orders/R601754941.json?token=... 3)localhost:4000/api/orders/R036622716.json?token=... 正确吗?我在问题的 URL 中硬编码了第一个,直到我弄清楚如何在 URL 中循环订单号
  • @Ignacio 我修复了答案,我忘了替换硬编码的订单号;它现在应该可以正常工作了(例如,在 URL 中进行 3 次调用 每个订单都有一个)。
  • 谢谢!这更有意义....现在我得到:# 的未定义方法 `do'
  • @Ignacio 哦,对不起,控制器中的另一个错字,第 5 行:orders.each.do |order|;它应该是orders.each do |order|(没有.)。我已经更新了答案。
  • 天哪,我读了 10 遍,我也错过了。非常感谢,它就像一个魅力!
猜你喜欢
  • 2017-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-02
  • 1970-01-01
  • 2012-01-31
相关资源
最近更新 更多