Activestorage 图像保存在 blob 中但未显示在视图中
Activestorage image saved in blob but not showing in view
我正在遵循 rails ActiveStorage 指南。我已成功将图像保存到本地数据库中的 blob table(附件 table 为空)。但我似乎无法在视图中显示它:
<%= url_for(@comment.images) if @comment.images %>
不起作用,也不起作用:
<% @comments.each do |c| %>
<%= c.body %>
<%= url_for(c.images) if c.images %>
<% end %>
显示评论作品的其他值,所以这是特定于图像的。我还尝试将 .url 添加到“图像”。我试过 image_tag 而不是 url_for。并循环浏览图像。和 image_tag url_for (而不是只使用其中一个)。并使用表示。在所有这些情况下都没有运气。如果有任何帮助,我将不胜感激。
还有我的模特
has_many_attached :images
application.js
//= require activestorage
import Rails from "@rails/ujs"
import Turbolinks from "turbolinks"
import * as ActiveStorage from "@rails/activestorage"
import "channels"
Rails.start()
Turbolinks.start()
ActiveStorage.start()
控制器
private
def comment_params
params.require(:comment).permit(:body, images: [])
end
你指定了has_many_attached,所以@comment.images会包含一个图片数组,所以下面的代码会显示你评论的第一张图片。
<%= image_tag(@comment.images.first) if @comment.images.attached? %>
如果您想显示所有图像,您可以遍历@comment.images 数组并显示每张图像
解决方案是更改文件上传表单
<%= form.file_field :images, multipart: true, direct_upload: true %>
至
<%= form.file_field :images, multiple: true, direct_upload: true %>
之前没有上传,有人建议改成“multipart”。解决了外观上的上传,但是没有附上图片。
我想上传外观现在也能用了,因为我疯狂地尝试了很多东西,我调整了一些东西。
现在附件 table 正在填充图像。之前只有 blob table 被填充。
我正在遵循 rails ActiveStorage 指南。我已成功将图像保存到本地数据库中的 blob table(附件 table 为空)。但我似乎无法在视图中显示它:
<%= url_for(@comment.images) if @comment.images %>
不起作用,也不起作用:
<% @comments.each do |c| %>
<%= c.body %>
<%= url_for(c.images) if c.images %>
<% end %>
显示评论作品的其他值,所以这是特定于图像的。我还尝试将 .url 添加到“图像”。我试过 image_tag 而不是 url_for。并循环浏览图像。和 image_tag url_for (而不是只使用其中一个)。并使用表示。在所有这些情况下都没有运气。如果有任何帮助,我将不胜感激。
还有我的模特
has_many_attached :images
application.js
//= require activestorage
import Rails from "@rails/ujs"
import Turbolinks from "turbolinks"
import * as ActiveStorage from "@rails/activestorage"
import "channels"
Rails.start()
Turbolinks.start()
ActiveStorage.start()
控制器
private
def comment_params
params.require(:comment).permit(:body, images: [])
end
你指定了has_many_attached,所以@comment.images会包含一个图片数组,所以下面的代码会显示你评论的第一张图片。
<%= image_tag(@comment.images.first) if @comment.images.attached? %>
如果您想显示所有图像,您可以遍历@comment.images 数组并显示每张图像
解决方案是更改文件上传表单
<%= form.file_field :images, multipart: true, direct_upload: true %>
至
<%= form.file_field :images, multiple: true, direct_upload: true %>
之前没有上传,有人建议改成“multipart”。解决了外观上的上传,但是没有附上图片。
我想上传外观现在也能用了,因为我疯狂地尝试了很多东西,我调整了一些东西。
现在附件 table 正在填充图像。之前只有 blob table 被填充。