如何列出带有载波的物体的每个图像
How to list every image for the object with carrierwave
我想在显示页面上显示用户上传的每张图片。阅读文档并观看一些视频,我能够创建上传器并使其工作。 (也许这很明显,但对我来说意义重大)。我查看了 Gem 文档,但没有找到具体说明。我尝试对每个对象做一些但没有用。
这是我的模型
class Imovel < ApplicationRecord
belongs_to :bairro
belongs_to :tipoimovel
has_many :caracteristica_imovels, inverse_of: :imovel
has_many :caracteristicas, through: :caracteristica_imovels, dependent: :destroy
accepts_nested_attributes_for :caracteristica_imovels, reject_if: :all_blank, allow_destroy: true
mount_uploaders :imagens, ImagemUploader
end
这是上传代码:
class ImagemUploader < CarrierWave::Uploader::Base
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
这是我的 _form 及其上传方式
<%= f.file_field :imagens, multiple: true, class: "float-left" %>
我正在显示的展示页面:
<%= image_tag(@imovel.imagens[1].url) if @imovel.imagens? %>
问题来了,我有数组索引,但我不知道如何让它动态工作。
我该怎么做?
在@max 的帮助下,正确的方法是使用 each 方法,正确执行。代码如下:
<% @imovel.imagens.each do |imagem| %>
<%= image_tag(imagem.url) %>
<% end %>
我想在显示页面上显示用户上传的每张图片。阅读文档并观看一些视频,我能够创建上传器并使其工作。 (也许这很明显,但对我来说意义重大)。我查看了 Gem 文档,但没有找到具体说明。我尝试对每个对象做一些但没有用。
这是我的模型
class Imovel < ApplicationRecord
belongs_to :bairro
belongs_to :tipoimovel
has_many :caracteristica_imovels, inverse_of: :imovel
has_many :caracteristicas, through: :caracteristica_imovels, dependent: :destroy
accepts_nested_attributes_for :caracteristica_imovels, reject_if: :all_blank, allow_destroy: true
mount_uploaders :imagens, ImagemUploader
end
这是上传代码:
class ImagemUploader < CarrierWave::Uploader::Base
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
这是我的 _form 及其上传方式
<%= f.file_field :imagens, multiple: true, class: "float-left" %>
我正在显示的展示页面:
<%= image_tag(@imovel.imagens[1].url) if @imovel.imagens? %>
问题来了,我有数组索引,但我不知道如何让它动态工作。 我该怎么做?
在@max 的帮助下,正确的方法是使用 each 方法,正确执行。代码如下:
<% @imovel.imagens.each do |imagem| %>
<%= image_tag(imagem.url) %>
<% end %>