9
6

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 で has_many なフォーム ( nested resouces ) を作るための最小構成

Last updated at Posted at 2015-11-18

前提 : 必要なマイグレーションは実行されていることとする。

親モデル

has_many と一緒に accepts_nested_attributes_for を指定すること。

app/models/book.rb
class Book < ActiveRecord::Base
  has_many :images
  accepts_nested_attributes_for :images
end

子モデル

モデルを作るだけで良い。

app/models/image.rb
class Image < ActiveRecord::Base
end

リソース

ポイント : permit_params に attributes を指定すること。

app/admin/book.rb
ActiveAdmin.register Book do

  permit_params :title, images_attributes: [:kind]

  form do |f|
    f.inputs do
      f.input :title
    end

    f.inputs do
      f.has_many :images do |t|
        t.input :kind
      end
    end

    f.actions
  end
end

環境

  • ActiveAdmin 1.0.0.pre1
  • Rails 4.0.13
  • ruby 2.0.0

参考

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

メンター受付

9
6
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
9
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?