Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: Active Admin Association Image column

I am new to ruby on rails and have just installed active admin and was trying to customize the

views.

I have a product and image table. Each image belongs to one product.

Now I want to display a column with the associated image when showing the products page.

At the moment it´s only the image_url text which isn´t working. Later on I would like do

have the picture displayed in 50x50px.

How to I do this? (image model: name:string image_url:text)

Here is what I have done :

ActiveAdmin.register Product do

index do
    column "Image" do |image|
        image.image_url
    end
    column :name
    column :preview_text
    column :full_text
    column :price, :sortable => :price do |product|
      div :class => "price" do
          number_to_currency product.price
     end
    end
 default_actions
 end
end

I dont know how to fix that part with " do image. I am a beginner in rails 2 days exp..

It seems the syntax is wrong and throwing error:

undefined method `image_url' for #<Product:0x00000101b5a458>

Thanks

like image 750
zer02 Avatar asked May 19 '13 23:05

zer02


1 Answers

You need to get the image_url from the image object product.image.image_url.

Regarding the image size, you can display the image with the size you want, like so

column "Image" do |product|
  image_tag product.image.image_url, class: 'my_image_size'
end

.css

.my_image_size {
  width: 50px;
  height: 50px;
}

Or you can actually resize the image itself, with a gem such as CarrierWave.

like image 144
Luís Ramalho Avatar answered Oct 06 '22 10:10

Luís Ramalho