Ruby on Rails |我想从 ActiveStorage 中获取 Best Seller Taxonomy 的图像 标题已获取并显示在屏幕上,但图像不是

Posted

技术标签:

【中文标题】Ruby on Rails |我想从 ActiveStorage 中获取 Best Seller Taxonomy 的图像 标题已获取并显示在屏幕上,但图像不是【英文标题】:Ruby on Rails | I want to fetch Images of Best Seller Taxonomy from ActiveStorage Title is Fetched and display on Screen But Images are not 【发布时间】:2022-01-15 17:54:41 【问题描述】:
<%best_sellers = Spree::Taxonomy.find_by_name("Best Seller").root.products.limit(6)%> 
<%best_sellers.each do |best_seller_array|                                             
   <div class="card card-custom text-center rounded w-25">
     <%= image_tag main_app.url_for(best_seller_array&.attachment),class:"img-fluid" %>
        <a href=""><%= best_seller_array.name%> </a>
   </div>
<%end%> 

【问题讨论】:

【参考方案1】:

在大礼包中,产品has_many :images,所以如果你想显示任何产品图像,你可以尝试如下 -

<%= image_tag main_app.url_for(product&.images&.first&.attachment), class: "img-fluid" %>

如果您想显示产品的所有图像,则只需遍历如下所示的图像循环

<% best_sellers_products.each do |product| %>
  <% product.images.each do |image| %>
     // display image
  <% end %>
<% end %>

希望这能解决问题。

谢谢

【讨论】:

以上是关于Ruby on Rails |我想从 ActiveStorage 中获取 Best Seller Taxonomy 的图像 标题已获取并显示在屏幕上,但图像不是的主要内容,如果未能解决你的问题,请参考以下文章