Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display images in the list action of rails_admin?

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?

like image 956
Mythical Fish Avatar asked Dec 08 '22 22:12

Mythical Fish


2 Answers

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
like image 173
Steve Davis Avatar answered Jan 06 '23 00:01

Steve Davis


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
like image 32
lucas teles Avatar answered Jan 06 '23 00:01

lucas teles