33
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 3 years have passed since last update.

Rails: ActiveAdmin.register_page でのアクションの作り方とはまりどころ。

Last updated at Posted at 2015-09-03

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 indexcontent 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オープンチャットもご利用ください。

メンター受付

33
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
33
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?