0
2

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.

ActiveAdminで二重にnestしたフォームを書く

Posted at

ActiveAdminでhas_oneのassociationに対するnestしたフォームを作る
に続き、ActiveAdminネタ。二重にネストしたフォームは普通にActiveAdminでも書けます。

親モデルParent、子モデルChild、孫モデルGrandChildが以下のように定義されているときに、

class Parent < ActiveRecord::Base
  has_many :childs # childの複数形はchildrenだけど、無視
  accepts_nested_attributes_for :childs, allow_destroy: true
end

class Child < ActiveRecord::Base
  belongs_to :parent
  has_many :grand_childs
  accepts_nested_attributes_for :grand_childs, allow_destroy: true
end

class GlandChild < ActiveRecord::Base
  belongs_to :child  
end

二重にネストしたフォームは以下のように書けます。ただし、当然ながら、 accepts_nested_attributes_forが正しく設定されていないと、内部的に呼ばれるnew_record?のところでnilが起因するエラーが出ます。

/admin/models/parent.rb
ActiveAdmin.register Parent do
  form do |f|
    f.inputs "子モデルの編集"
     f.has_many :childs do |child|       
       child.input :hoge

       child.has_many :grand_childs do |grand_child|
         grand_child.input :moge
       end
     end
    end
  end

end
0
2
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
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?