setoya-blog

システム開発技術、データ分析関連でお勉強したことや、山奥生活を綴る、テンション低めなブログです。

rails4でActiveAdminの編集フォームでStrongParametersを指定する方法

ActiveAdminは1.0からRails4でも使えるが、Rails3のときの使い方と比べて多少変えるべき所がある。

StrongParametersを指定するコード例

まずはActiveAdminのモデル登録用ファイルに、StrongParametersの指定をする。ネストしたフォームの場合も通常のコントローラーで実装する通りにparmit_paramsを指定すればOK。

ActiveAdmin.register Post do
  permit_params :title, :content, :publisher_id,
    tags_attributes: [:id, :name, :description, :_destroy]
end

ネストしたフォームの場合、モデルの方にもaccepts_nested_attributes_forの設定が必要。

# Note that `accepts_nested_attributes_for` is still required:
class Post < ActiveRecord::Base
  accepts_nested_attributes_for :tags, allow_destroy: true
end

参考

公式リポジトリのMarkdown