【发布时间】:2018-07-25 08:25:37
【问题描述】:
我正在尝试使用 Rails 在 Shopify 中获取产品图像的“src”。
我在控制器中执行此操作:
@products = ShopifyAPI::Product.find(:all)
我想获得的每个产品的数据是它的标题和图片,所以基本上我在视图中这样做:
<% @products.each do |product| %>
<%= product.title %>
<%= product.images[0].src %>
<%= end %>
此行不起作用: 因为 product.images[0] 返回此对象:ShopifyAPI::Image:0x007fd6f4092338
Product.images:
"图片"=>[#943579299853, "位置"=>1, "created_at"=>"2018-01-16T12:32:53-05:00", “updated_at”=>“2018-01-16T12:32:53-05:00”,“宽度”=>5384, “身高”=>3589, "src"=>"https://cdn.shopify.com/s/files/1/2396/9643/products/playing-guitar-fretboard.jpg?v=1516123973", "variant_ids"=>[]}, @prefix_options={:product_id=>299776016397}, @persisted=true>]
我怎样才能得到'src'值?
谢谢。
编辑::
product.images[0].attributes['src']
完美运行!但是,如果产品没有附加任何图像,则会引发错误,因此首先您必须检查您的产品是否有图像(如果图像[0] == null)
【问题讨论】: