1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ActiveAdminでPaperclipの画像を表示する

Last updated at Posted at 2015-09-30

概要

Paperclipを使って保存した画像を、ActiveAdminのIndexページとShowページで表示する

前提条件

  • ActiveAdminとPaperclipを使用していること

実装

# app/model/capture_image.rb

class CaptureImage < ActiveRecord::Base
  has_attached_file :image,
    :path   => "/:class/:id/:attachment/:style/:filename",
    :styles => {
      :thumb    => '200x200>',
      :original => '900x900>'
    }

  validates_attachment :image, :presence => true,
    :content_type => { :content_type => /\Aimage\/.*\Z/ },
    :size => { :in => 0..1.megabytes }
end
# app/admin/capture_image.rb:

ActiveAdmin.register CaptureImage do
  index do
    column :thumb do |obj|
      image_tag(obj.image.expiring_url(10, :thumb))
    end
    actions
  end

  show do
    attributes_table do
      row :image do |obj|
        image_tag(obj.image.expiring_url(10))
      end
    end
    active_admin_comments
  end
end
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?