9

我正在使用回形针上传多张图片。我也在使用 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 张图片与每个产品相关联,我在我的展示块中使用上面的代码。请提出一个更好的方法来做到这一点。

4

1 回答 1

16

你的意思是你需要显示每一个图像而不仅仅是其中的 3 个?如果是,请尝试

row "Images" do
   ul do
    product.images.each do |img|
      li do 
        image_tag(img.image.url(:small))
      end
    end
   end
end
于 2013-04-18T15:55:51.943 回答