如何在HTML

时间:2017-03-23 23:09:52

标签: html ruby-on-rails ruby shopify

我收到一个要在Rails中显示的Shopify对象。

在我的HTML中,我有这段代码:

<ul>
  <% @customers.each do |customer| %>
    <li><%= customer.orders %></li>
  <% end %>
</ul>

然后我得到了这个输出:

[#<ShopifyAPI::Order:0x007f90f53117c0 @attributes={"id"=>4889340686, "email"=>"", "closed_at"=>nil, "created_at"=>"2017-03-21T20:09:41-04:00", "updated_at"=>"2017-03-21T20:09:41-04:00", "number"=>3, "note"=>"", "token"=>"74b8cae748de2154dae08edc4cb9b0b8", "gateway"=>"manual", "test"=>false, "total_price"=>"121.00", "subtotal_price"=>"110.00", "total_weight"=>0, "total_tax"=>"11.00", "taxes_included"=>false, "currency"=>"AUD", "financial_status"=>"paid", "confirmed"=>true, "total_discounts"=>"0.00", "total_line_items_price"=>"110.00", "cart_token"=>nil, "buyer_accepts_marketing"=>false, "name"=>"#1003", "referring_site"=>nil, "landing_site"=>nil, "cancelled_at"=>nil, "cancel_reason"=>nil, "total_price_usd"=>"93.51", "checkout_token"=>nil, "reference"=>nil, "user_id"=>107636622, "location_id"=>40480334, "source_identifier"=>nil, "source_url"=>nil, "processed_at"=>"2017-03-21T20:09:41-04:00", "device_id"=>nil, "browser_ip"=>nil, "landing_site_ref"=>nil, "order_number"=>1003, "discount_codes"=>[], "note_attributes"=>[], "payment_gateway_names"=>["manual"], "processing_method"=>"manual", "checkout_id"=>nil, "source_name"=>"shopify_draft_order", "fulfillment_status"=>nil, "tax_lines"=>[#<ShopifyAPI::TaxLine:0x007f90f9e49ee0 @attributes={"title"=>"GST", "price"=>"11.00", "rate"=>0.1}, @prefix_options={}, @persisted=true>], "tags"=>"", "contact_email"=>nil, "order_status_url"=>nil, "line_items"=>[#<ShopifyAPI::LineItem:0x007f90f9e492b0 @attributes={"id"=>9182800654, "variant_id"=>37496676110, "title"=>"Expensive Product", "quantity"=>1, "price"=>"100.00", "grams"=>0, "sku"=>"", "variant_title"=>nil, "vendor"=>"Gronade Dev Test", "fulfillment_service"=>"manual", "product_id"=>10105372366, "requires_shipping"=>true, "taxable"=>true, "gift_card"=>false, "name"=>"Expensive Product", "variant_inventory_management"=>nil, "properties"=>[], "product_exists"=>true, "fulfillable_quantity"=>1, "total_discount"=>"0.00", "fulfillment_status"=>nil, "tax_lines"=>[#<ShopifyAPI::TaxLine:0x007f90f3933650 @attributes={"title"=>"GST", "price"=>"10.00", "rate"=>0.1}, @prefix_options={}, @persisted=true>]}, @prefix_options={}, @persisted=true>, #<ShopifyAPI::LineItem:0x007f90f3932cf0 @attributes={"id"=>9182800718, "variant_id"=>37496692110, "title"=>"Cheap Product", "quantity"=>1, "price"=>"10.00", "grams"=>0, "sku"=>"", "variant_title"=>nil, "vendor"=>"Gronade Dev Test", "fulfillment_service"=>"manual", "product_id"=>10105380174, "requires_shipping"=>true, "taxable"=>true, "gift_card"=>false, "name"=>"Cheap Product", "variant_inventory_management"=>nil, "properties"=>[], "product_exists"=>true, "fulfillable_quantity"=>1, "total_discount"=>"0.00", "fulfillment_status"=>nil, "tax_lines"=>[#<ShopifyAPI::TaxLine:0x007f90f9e53be8 @attributes={"title"=>"GST", "price"=>"1.00", "rate"=>0.1}, @prefix_options={}, @persisted=true>]}, @prefix_options={}, @persisted=true>], "shipping_lines"=>[], "fulfillments"=>[], "refunds"=>[], "customer"=>#<ShopifyAPI::Customer:0x007f90f9e53238 @attributes={"id"=>5793410830, "email"=>nil, "accepts_marketing"=>false, "created_at"=>"2017-03-21T20:09:21-04:00", "updated_at"=>"2017-03-21T20:09:41-04:00", "first_name"=>"Benderditch", "last_name"=>"Cucumberpatch", "orders_count"=>1, "state"=>"disabled", "total_spent"=>"121.00", "last_order_id"=>4889340686, "note"=>nil, "verified_email"=>false, "multipass_identifier"=>nil, "tax_exempt"=>false, "phone"=>nil, "tags"=>"", "last_order_name"=>"#1003"}, @prefix_options={}, @persisted=true>}, @prefix_options={}, @persisted=true>]

如何构造此输出,或者更好地如何访问这些变量?我不知道如何处理@带注释的实例变量。

当我尝试输出这样的东西时:

<ul>
  <% @customers.each do |customer| %>
    <li><%= customer.orders.attributes %></li>
  <% end %>
</ul>

我收到错误。

1 个答案:

答案 0 :(得分:2)

由于customers.orders是一种排序字典(它实际上是一个ActiveRecord::Associations::CollectionProxy,所以你必须以环绕customers的方式循环它。就像这样:

<ul>
  <% @customers.each do |customer| %>
    <% customer.orders.each do |order| %>
      <li><%= order.whatever_field_you_want %></li>
    <% end %>
  <% end %>
</ul>