简体   繁体   English

Rails将插入的变量从数据库循环到url以进行外部调用

[英]Rails loop through inserted variable from database to url for external call

I'm trying to show the line_items of an order from Solidus into the Items table in my rails app. 我试图将来自Solidus的订单的line_items显示到我的rails应用程序的Items表中。 Previously I saved the orders from Solidus to my "Order" (in my app) table with attribute "order_number" 以前,我将订单从Solidus保存到属性为“ order_number”的“ Order”(在我的应用中)表中

I need to insert the order_number into the url I'm using for the external call using httparty and loop through all the rows in order_number to show the attributes of each order in my index view. 我需要将order_number插入到我使用httparty进行外部调用的url中,并循环遍历order_number中的所有行,以在索引视图中显示每个订单的属性。 Each order, for example: http://localhost:4000/api/orders/R984044507.json?token=fd5a8b4b294303ad52fcb19a9eaf8bb7325d20a8aa5abbc7 has a structure like this: 每个订单,例如: 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"
}
]
}

here is my controller: 这是我的控制器:

# 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

My Index looks like this: 我的索引如下所示:

<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>

I hard coded the order number of one of the orders in my Orders table to test that it shows the items in each order, but I don't know how to loop through the orders so I get the items from every order. 我在“订单”表中对其中一个订单的订单号进行了硬编码,以测试它是否显示了每个订单中的商品,但是我不知道如何遍历订单,因此我从每个订单中获取商品。

I'm not sure my approach is the best, I'm a beginner. 我不确定我的方法是最好的,我是初学者。 Any help will be appreciated. 任何帮助将不胜感激。

You could do something like this: 您可以执行以下操作:

Controller 调节器

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

First you get all order numbers (i assume that number is the column name in orders table) and then you loop through them, requesting each order and adding its attributes to @items array. 首先,您获得所有订单编号(我假设numberorders表中的列名称),然后遍历它们,请求每个订单并将其属性添加到@items数组。

View 视图

<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>

Since @items is an array you will need to iterate it too, so two loops are needed: one for @items and one for line_items . 由于@items是一个数组,因此您也需要对其进行迭代,因此需要两个循环:一个用于@items ,一个用于line_items

Although this solution should work, note that requesting a high number of orders could cause performance issues. 尽管此解决方案应该可行,但请注意,请求大量订单可能会导致性能问题。

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

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