gpt4 book ai didi

html - 使用 Rails 显示数据库中的图像

转载 作者:搜寻专家 更新时间:2023-10-30 20:09:13 24 4
gpt4 key购买 nike

我正在尝试将我“存储”在数据库中的特定类型的图像显示到 View 中。我有一个“产品”模型和一个“图像”模型,因为每种产品都有几种类型的图像(例如“图库”、“特色”、“拆箱图像”,它们简单地存储为类型 0 的属性, 1 或 2)

我已将其放入 HTML 中:

<%= image_tag @product.images.where(image_type: 2).first, :alt => 'product_image' %>

当我点击检查元素时,我看到了这个:

<img alt="product_image" src="/images/#<Image:0x007fcba329f4c0>">

有人可以解释为什么会这样吗?看起来它正在我的 assets/images 文件夹中搜索图像,但那里的 #< 之类的是什么?我的图像在数据库中存储为 lamp.png。

这是我的图像模型:

class Image < ActiveRecord::Base

include AASM

belongs_to :product
mount_uploader :file, FileUploader

validates :image_type, presence: true


enum image_type: [IMAGE_UNBOXING.to_s, IMAGE_GALLERY.to_s, IMAGE_FEATURED.to_s]

aasm column: :image_type do
Image.image_types.keys.each do |state_string|
state state_string.to_sym
end

end

end

谢谢。

最佳答案

@product.images.where(image_type: 2).first 从数据库返回第一条记录,但是对于 image_tag 你应该指定 url 或返回记录的 path

使用 url 属性或您拥有的属性:

<%= image_tag @product.images.where(image_type: 2).first.url, :alt => 'product_image' %>
^^^

关于html - 使用 Rails 显示数据库中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29843018/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com