【问题标题】:Image not showing for multiple image upload with active storage使用活动存储上传多张图片时未显示图片
【发布时间】:2019-12-24 16:30:57
【问题描述】:

我遵循了有关如何使用活动记录上传多个图像的教程。我已经安装了活动记录,将我的关联添加到我的模型中,当我创建服务(我的模型)时,当我上传 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>

我做 Service.last.images 时的 Rails c

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

我希望我的 3 张图片显示在 FE 上

【问题讨论】:

  • 当您在控制台中执行Service.last.images.count 时,您会得到 3 吗?
  • 你好@MZaragoza 是的,我愿意!我在前端所能看到的只是损坏的图像和来自控制台的活动记录字符串。
  • 在 ERB 文件上,如果您执行 ` img
  • 我在前端得到这个img img img [#&lt;ActiveStorage::Attachment id: 16, name: "images", record_type: "Service", record_id: 15, blob_id: 16, created_at: "2019-08-19 18:56:12"&gt;, #&lt;ActiveStorage::Attachment id: 17, name: "images", record_type: "Service", record_id: 15, blob_id: 17, created_at: "2019-08-19 18:56:13"&gt;, #&lt;ActiveStorage::Attachment id: 18, name: "images", record_type: "Service", record_id: 15, blob_id: 18, created_at: "2019-08-19 18:56:13"&gt;]

标签: ruby ruby-on-rails-3 activerecord


【解决方案1】:

这是我最终用来让图像显示在我的 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

【讨论】:

    猜你喜欢
    • 2016-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-26
    • 1970-01-01
    相关资源
    最近更新 更多