视图中未显示通过活动存储上传的图像

问题描述 投票:0回答:1

我正在尝试使用ActiveStorage将Rails应用程序升级到RoR6。在视图中,我在附件图像上使用了image_tag,在@project上使用了“ whiteboard_background”。

并且它在浏览器中显示一个断开的链接:Link

环境:开发和文件存储在本地

型号:

class Project < ApplicationRecord

  has_one_attached :whiteboard_background

查看代码:

attachment: <%= @project.whiteboard_background.attached? %>
<p>image:
<%= image_tag @project.whiteboard_background if @project.whiteboard_background.attached? %>
</p>

服务器:

Started GET "/projects/6" for 127.0.0.1 at 2020-02-19 20:29:27 +0100
Processing by ProjectsController#show as HTML
  Parameters: {"id"=>"6"}
...
ActiveStorage::Attachment Load (2.8ms)  SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4  [["record_id", 6], ["record_type", "Project"], ["name", "whiteboard_background"], ["LIMIT", 1]]
  ↳ app/views/projects/show.html.erb:94
  ActiveStorage::Blob Load (0.6ms)  SELECT "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = $1 LIMIT $2  [["id", 13], ["LIMIT", 1]]
  ↳ app/views/projects/show.html.erb:96
  Location Load (0.5ms)  SELECT "locations".* FROM "locations" ....

我已经检查过@project.whiteboard_background.attached?是否为true,并且在控制台中,Project.last.whiteboard_background.metadata提供了正确的图像数据,表明上传工作正常。

如果我从头开始在空白测试应用程序中重复该过程,它可以正常工作,并且我可以显示图像。

任何想法?

ruby-on-rails rails-activestorage
1个回答
0
投票

对于本地存储中的图像,请使用此

<%= url_for(@project.whiteboard_background) %>

如果它正在生产中并且在S3之类的产品上,那么您可能想要这样的产品

<%= @project.whiteboard_background.service_url %>
© www.soinside.com 2019 - 2024. All rights reserved.