首页 > 解决方案 > 使用活动存储上传多张图片时未显示图片

问题描述

我遵循了有关如何使用活动记录上传多个图像的教程。我已经安装了活动记录,将我的关联添加到我的模型中,当我创建服务(我的模型)时,当我上传 3 张图像时,一切似乎都在工作,即没有活动记录错误但是我在 FE 上看不到我的图像,我看到的都是是一个破碎的图像。

当我进入 Rails c 并输入 Service.images.last 时,我可以看到 images 为 nil。有人知道为什么吗?

你还有什么需要知道或从我这里看到的吗?

谢谢

我显示图像的代码:

<%=@service.images.each do  |img| %>
  <%= cl_image_tag @service.images, crop: :fill, class: 'card-image', class: 'card-image-show'%>
 <%end %>

导轨 c

  Service Load (59.6ms)  SELECT  "services".* FROM "services" ORDER BY "services"."id" DESC LIMIT $1  [["LIMIT", 1]]
=> #<ActiveStorage::Attached::Many:0x00007fffc3ff08a0 @name="images", @record=#<Service id: 14, name: "Test Carpet cleaning", description: "Lorem Ipsum is simply dummy text of the printing a...", picture_url: nil, video: nil, category: "carpet cleaning", created_at: "2019-08-19 12:32:35", updated_at: "2019-08-19 12:32:35", photo: nil, images: nil>, @dependent=:purge_later>

Rails c 当我做 Service.last.images

irb(main):013:0> Service.last.images
  Service Load (0.6ms)  SELECT  "services".* FROM "services" ORDER BY "services"."id" DESC LIMIT $1  [["LIMIT", 1]]
=> #<ActiveStorage::Attached::Many:0x00007f92fd1f8780 @name="images", @record=#<Service id: 15, name: "test multiple images", description: "Lorem Ipsum is simply dummy text of the printing a...", picture_url: nil, video: nil, category: "carpet cleaning", created_at: "2019-08-19 18:56:12", updated_at: "2019-08-19 18:56:13", photo: nil, images: nil>, @dependent=:purge_later>

服务模式

class Service < ApplicationRecord
  has_many_attached :images
  mount_uploader :photo, PhotoUploader
end

我想在 FE 上显示我的 3 张图片

标签: rubyruby-on-rails-3activerecord

解决方案


这是我最终用来让图像显示在我的 SHOW PAGE 上的代码。

<% if @service.images.attached? %>
<p>
  <strong>Images:</strong>
  <br>
  <% @service.images.each do |image| %>
    <%= image_tag(image) %>
  <% end %>
</p>
<% end %>

cl_image_tag用来显示我的图像导致图像显示损坏。我应该用上面的。

资料来源:https ://evilmartians.com/chronicles/rails-5-2-active-storage-and-beyond


推荐阅读