LoginSignup
26
26

More than 5 years have passed since last update.

[Rails 4.x] ActiveAdmin をカスタマイズして、indexやshowでassociation関係のテーブルのカラムも表示させる

Last updated at Posted at 2014-10-22

アソシエーションの確認

app/models/product.rb
class Product < ActiveRecord::Base
  belongs_to :maker
end 
app/models/maker.rb
class Maker < ActiveRecord::Base
  has_many :products
end 

indexページをカスタマイズ

以下を追記。

app/admin/product.rb
ActiveAdmin.register Product do
  index do
    selectable_column

    column :id do |product|
      link_to product.id, admin_product_path(product)
    end
    column :name do |product|
      link_to product.name, admin_product_path(product)
    end
    column :description
    column :maker do |product|
      if product.maker.present?
        link_to product.maker.name, admin_maker_path(product.maker)
      else
        status_tag('Empty')
      end
    end
  end
end

showページをカスタマイズ

以下を追記。

app/admin/product.rb
ActiveAdmin.register Product do
  show do |f|
    panel "Subject" do
      attributes_table_for f, :id,:name,:description,:maker
    end
  end
end

参考

26
26
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
26
26