How to display images in the list action of rails_admin?
Asked Answered
M

2

5

I have a self-associating model which allows the user to define a "parent photo", in order to group them together.

My model:

class Photo < ActiveRecord::Base

  attr_accessible :image, :parent_photo

  has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }

  validates_attachment :image, 
    :presence => true,
    :size => { :in => 20..2000.kilobytes },
    :content_type => { :content_type => [ 'image/jpeg', 'image/png' ] }

  belongs_to :parent, class_name: "Photo", foreign_key: :parent_photo

  def associated_photos
    Photo.find_by_parent_photo(id)
  end

end

And in my rails_admin initializer:

..

  config.model Photo do

    list do
      field :image
      field :associated_photos     
    end
  end
end

How do I retrieve the actual thumbnail for use in the list action?

Merrick answered 18/7, 2012 at 22:18 Comment(0)
E
9

It seems like this is addressed on the wiki under the heading "Fields - Output formatting."

Will something like this work?

config.model Photo do
  list do
    field :image do
      formatted_value do
        bindings[:view].tag(:img, { :src => bindings[:object].image.url }) << value
      end
    field :associated_photos     
  end
end
Exploration answered 26/7, 2012 at 23:59 Comment(1)
This worked for me (using just .image instead of .image.url). Thanks!Marque
S
6

To show the image field:

config.model Photo do
  list do
    field :image do
      pretty_value do
        bindings[:view].tag(:img, { :src => bindings[:object].image.url(:thumb) })
      end
    end
  end
end
Sardius answered 2/2, 2016 at 17:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.