0

我正在尝试使用导轨获取 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)

4

2 回答 2

1

你试过<%= product.images[0].attributes['src'] %>吗?

于 2018-02-15T00:14:22.517 回答
1

我建议查看此博客以更好地访问您的产品图片及其各种属性。它们还包括示例代码。祝你好运!

于 2018-03-05T16:55:01.423 回答