ActiveAdmin の register_page (モデルに関連付けない書き方) でほんとに苦労したのでメモ。
ポイント
- アクションの追加は
page_action
でおこなう。page_action
を書かないとルーティングさえ追加されない!(でもなぜか、index アクションだけは定義しなくても最初からある) - ActiveAdmin.register では
index do
にあたる部分が、register_profile ではcontent do
になっている! (わかりづらい) - たとえば
index do
の中で@text = 'ABCD'
って書いても、content do
の中ではそのまま使えない! text っていう普通の変数になっている。 - レイアウトの変更は
controller do
の直下に書く!def index
とかの中には書かない。 -
def index
とcontent do
が対応している。 ではたとえばdef edit
に対応するものは何なのか? たぶん page_action :edit do だ。(未確定)
ActiveAdmin.register_page "AuthorPage" do
content do
panel 'メニュー' do
link_to '新規作成', admin_authorpage_new_path
end
table_for authors do
column "名前", :name
column "性別", :gender
column ("name_changed"){ |author| author.name_changed }
column ("編集"){ |author| link_to('編集する', admin_authorpage_edit_path(id: author.id)) }
end
end
page_action :new, method: :get
page_action :create, method: :post
page_action :update, method: :post
page_action :edit, method: :get do
end
controller do
layout 'active_admin', only: [:edit, :new]
def index
@authors = Author.all
end
def new
end
def create
Author.create(params.require(:author).permit(:name, :gender))
flash[:notice] = '作成しました。'
redirect_to admin_authorpage_path
end
def update
flash[:notice] = '編集しました。'
author = Author.find(params[:author][:id])
author.update(params.require(:author).permit(:name, :gender))
redirect_to admin_authorpage_edit_path(id: params[:author][:id])
end
def edit
@author = Author.find(params[:id])
end
end
end
チャットメンバー募集
何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。
メンター受付