32
29

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.

Rails Activeadminカスタマイズメモ

Last updated at Posted at 2016-12-12

Railsのgem、ActiveAdminを使って管理画面を構築する際に利用した機能のメモ。
※随時更新予定。

ダウンロードリンクを消す。

index download_links: false

全てのparameterを許可する。

permit_params { User.attribute_names.map(&:to_sym) }

カスタマイズしたカラムにソート機能を追加する。


class User < ApplicationRecord
  has_many :messages, dependent: :destroy
end

class Message < ApplicationRecord
  belongs_to :user, counter_cache: :messages_count
end

controller do
  def scoped_collection
    super.joins('LEFT JOIN "messages" ON "messages"."user_id" = "users"."id"')
         .select('users.*, count(messages.*) as message_count')
         .group('users.id')
  end
end

index do
  column 'メッセージ総数', :message_count, sortable: :message_count
end

newとeditでformの内容を変える。

attributes = User.attribute_names.map(&:to_sym)
attributes.delete(:id)
attributes.each do |attribute|
  if f.object.new_record?
    f.send(:input, attribute)
  else
    f.send(:input, attribute, input_html: { disabled: "disabled" })
  end
end

member_actionで設定したpageのタイトル編集方法。

member_action :report, method: :get do
  @page_title = "レポート"
end

リソースの追加。

rails generate active_admin:resource MyModel

ページネーションの削除。

config.paginate = false

ページネーションの日本語化

views:
  pagination:
    first: "&laquo; 最初"
    last: "最後&raquo;"
    previous: "&lsaquo; 前"
    next: "次 &rsaquo;"
    truncate: "..."

レコード数が多いテーブルのindexページが重い時は select count(*) from table を消す

index pagination_total: false do
  id_column
  column :user_id
  ...
end

パネルを横並びに表示する

columns do
  column do
    panel "hogehoge" do
    end
  end
  
  column do
    panel "fugafuga" do
    end
  end
end

編集画面にエラーメッセージを表示させたい

form do |f|
  f.semantic_errors *f.object.errors.keys
  f.inputs do
    f.input :name
  end
end
32
29
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
32
29

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?