ruby-on-rails - 资产管道中不存在资产“ ”
问题描述
如果我没有在产品中添加任何图像,那么它可以与默认图像一起正常工作,但是如果我使用 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 %>
解决方案
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 表中。
推荐阅读
- python - 将列表的每个整数值转换为熊猫系列下的字符串
- eclipse - 在纸莎草纸中创建序列图的问题
- bdd - 如果我的个人工作 BDD 测试用例在标记模式下运行,为什么它们不起作用?
- java - Windows 10 缩放与绘图尺寸混淆
- javascript - 从 Dropzone.js 获取文件到 PHP 控制器
- flutter - TextField 内的后退按钮
- c# - 使用 mock 对我的 API 进行单元测试,并引用多个存储库
- python - 在 QTreeWidget 中的 QTreeWidgetItems 上使用多种高亮颜色
- python - 如何将“curl -X POST -T”转换为 python
- android-studio - 如何删除错误消息:Android Studio 中的“未使用的导入语句”?