我正在尝试使用导轨获取 Shopify 中产品图像的“src”。
我在控制器中这样做:
@products = ShopifyAPI::Product.find(:all)
我想获得的每个产品的数据是它的标题和图像,所以基本上我在视图中这样做:
<% @products.each do |product| %>
<%= product.title %>
<%= product.images[0].src %>
<%= end %>
此行不起作用:<%= product.images[0].src %>因为 product.images[0] 返回此对象:ShopifyAPI::Image:0x007fd6f4092338
产品图片:
"images"=>[#< ShopifyAPI::Image:0x007fd6f4092338 @attributes={"id"=>943579299853, "position"=>1, "created_at"=>"2018-01-16T12:32:53-05: 00", "updated_at"=>"2018-01-16T12:32:53-05:00", "width"=>5384, "height"=>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}, @坚持=真>]
我怎样才能得到'src'值?
谢谢你。
编辑::
product.images[0].attributes['src']
完美运行!但是,如果产品没有附加任何图像,则会引发错误,因此首先您必须检查您的产品是否有图像(如果图像[0] == null)