Rails & Carrierwave: 如何在不同控制器输出img_tag?

Rails & Carrierwave: How to output img_tag in different controller?

我想知道如何在外国 controller/view 上输出 img_tag。我在 Rails 中生成了一个 Carrierwave 上传器并将其安装到特定型号。现在我想在不同控制器的视图中访问上传的图像。我得到的只是一个像 /images/%23%3CImage::ActiveRecord_Relation:0x00000004a503a8%3E.

这样的输出

在图像模型中,上传器安装了

mount_uploader :image, ImageUploader

我尝试获取 img_path 或创建 img_tag,但无法获得有效结果。例如我这样做:

<div class="parallax-container">
  <% @image = Image.where(category: 'Parallax', id: :id) %>
  <div class="parallax"><%= image_path(@image) %></div>
</div>

debug params 输出显示我的控制器是 games,例如 id: 在特定页面上是 1。因此,即使我更改 where 哈希并告诉它使用 id: '1',我也不会获得正确的图像路径。

如果我改为使用 <%= image_tag(@image) %>,则会为 SRC 和 ALT 参数创建相同的 ActiveRecord-Relation-Output。

当我尝试使用 <%= image_tag @image.img_url %> 时,我被告知 img_url 是一个未定义的方法。

游戏和图片是有关系的,一个游戏有很多图片,一张图片属于一个游戏。

我还尝试向游戏节目控制器添加一个实例变量,但它并没有改变结果。

where method return an array of records ActiveRecord_Relation. If you have an id of the record which you need, you should use find 方法。

<% @image = Image.where(category: 'Parallax').find(id_of_the_record) %>

您也可以使用相同的find方法按类别查找图像:

<% @image = Image.find_by(category: 'Parallax') %>