我正在使用Paperclip上传多个图像。 我也在使用Active Admin。 到目前为止,我已经能够上传多个图像并将它们关联到我的“产品”模型。 我还可以在索引页面上显示所有关联图像的名称。 但是,我无法找到如何在产品模型的展示页面中显示“所有”图像(而不仅仅是名称)。 请查看以下代码。
\app\models\product.rb
has_many :images, :dependent => :destroy
accepts_nested_attributes_for :images, :allow_destroy => true
\app\models\image.rb
belongs_to :product
has_attached_file :image, :styles => {
:thumb=> "100x100>",
:small => "300x300>",
:large => "600x600>"
}
\app\admin\products.rb
index do
column "Images" do |product|
product.images.map(&:image_file_name).join("<br />").html_safe
end
end
show do |product|
attributes_table do
row "Images" do
ul do
li do
image_tag(product.images.first.image.url(:small))
end
li do
image_tag(product.images.second.image.url(:small))
end
li do
image_tag(product.images.last.image.url(:small))
end
end
end
end
end
我找到了一种可行的方法,但这是非常糟糕的编程。目前,每个产品都有3个相关联的图像,并且我正在在我的展示块中使用上述代码。 请建议更好的做法。
image_tag(product.images.first.image.url(:small)) if !product.images.first.nil?
然而,这段代码仍然有一个最多只能显示3张图片的限制。请建议如何更好地实现此功能。 - ansh0809