更新 activeadmin 中的关系

Update a relation in activeadmin

我有两个模型。

fb_page.rb

has_one :fb_page_template, dependent: :destroy
accepts_nested_attributes_for :fb_page_template, :allow_destroy => false

fb_page_template.rb

belongs_to :fb_page
has_many :subscriptions

活跃管理员

ActiveAdmin.register FbPage do
  form title: 'Facebook page form' do |f|
    f.has_many :fb_page_template, new_record: false, allow_destroy: false do |k|
        k.input :subscribed
    end
  end
end

现在,当我尝试更新表单时,它会尝试删除订阅和 fb_page_template。

我只想更新 fb_page_template

subscribed 的值

我认为您在这里遗漏了一些东西:

  • 您需要在 fb_page_template.rb

  • 中允许 accepts_nested_attributes_for :subscriptions, :allow_destroy => false
  • 您也需要在 ActiveAdmin 中允许所有嵌套属性。

  • 您需要嵌套表格。

这是我的 fb_pages.rb ActiveAdmin 中的内容:

ActiveAdmin.register FbPage do
  permit_params :attribute_name_for_fb_page, 
                fb_page_template_attributes: [
                  :id, :fb_page_id, :attribute_name_for_fb_page_template,
                  subscriptions_attributes: [
                    :subscribed,
                    :fb_page_template_id
                  ]
                ]

  form title: "Facebook page form" do |f|
    f.inputs do 
      f.input :attribute_name_for_fb_page 

      f.has_many :fb_page_template, allow_destroy: false do |t|
        t.input :attribute_name_for_fb_page_template

        t.has_many :subscriptions do |s|  
          s.input :subscribed, as: :boolean
        end
      end
    end

    f.actions
  end
end

这就是我在 fb_page_template.rb

class FbPageTemplate < ApplicationRecord
  belongs_to :fb_page
  has_many :subscriptions
  accepts_nested_attributes_for :subscriptions, :allow_destroy => false
end

希望这对你有用。