首页 > 解决方案 > 资产管道中不存在资产“ ”

问题描述

错误信息截图

如果我没有在产品中添加任何图像,那么它可以与默认图像一起正常工作,但是如果我使用 spree 管理面板添加图像,那么在前端我会遇到这个问题。

内部:- app/helpers/spree/trackers_helper.rb

module Spree
  module TrackersHelper
    def product_for_segment(product, optional = {})
      {
        product_id: product.id,
        sku: product.sku,
        category: product.category.try(:name),
        name: product.name,
        brand: product.brand.try(:name),
        price: product.price,
        currency: product.currency,
        url: product_url(product),
      }.tap do |hash|
        hash[:image_url] = asset_url(optional.delete(:image).attachment) if optional[:image]
      end.merge(optional).to_json.html_safe
    end
  end
end

内部:- app/views/spree/shared/_products.html.erb

<% content_for :head do %>
  <% if products.respond_to?(:total_pages) %>
    <%= rel_next_prev_link_tags products %>
  <% end %>
<% end %>

<div data-hook="products_search_results_heading">
  <% if products.empty? %>
    <div data-hook="products_search_results_heading_no_results_found">
      <%= Spree.t(:no_products_found) %>
    </div>
  <% elsif params.key?(:keywords) %>
    <div data-hook="products_search_results_heading_results_found">
      <h6 class="search-results-title"><%= Spree.t(:search_results, keywords: h(params[:keywords])) %></h6>
    </div>
  <% end %>
</div>

<% if products.any? %>
  <div id="products" class="row" data-hook>
    <%= render partial: 'spree/products/product', collection: products, locals: { taxon: @taxon } %>
  </div>
<% end %>

<% if products.respond_to?(:total_pages) %>
  <%= paginate products, theme: 'twitter-bootstrap-3' %>
<% end %>

标签: ruby-on-railsrubyspreespree-auth-devise

解决方案


Spree 正在使用 ActiveStorage 来存储图像文件。它们位于 spree 项目的存储文件夹中。以批处理方式上传它们的一种方法是使用其余 API

https://guides.spreecommerce.org/api/product_images.html

对于 rest-api,我一直在使用 unirest。

POST /api/v1/products/a-product/images

a-product =friendly_id (准确地说是slug名称),它们存储在friendly_id_slug表中。当您将图像上传到 Spree 时,Spree 将在该表中查找 slug 名称并将其上传到 ActiveStorage。记录将分别保存在 spree_assets、active_storage_blobs 和 active_storage_attachments 表中。


推荐阅读