I am using paperclip to upload multiple images. I am also using Active Admin. So far, I have been able to upload multiple images and associate them to my 'product' model. I am also able to display the "names" of all the associated images in the index page . However , I am unable to find out how to display 'all' the images (not just names) in my show page of the product model. Please find the code below.
\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
I have found something that works but this is really bad programming. I have currently 3 images associated to each product and I am using the above code inside my show block . Please suggest a better way to do this.
do you mean you need to display each one image not just 3 of them ? If yes try to
row "Images" do
ul do
product.images.each do |img|
li do
image_tag(img.image.url(:small))
end
end
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With