Ruby on rails 在activeadmin中更新关系

Ruby on rails 在activeadmin中更新关系,ruby-on-rails,activeadmin,Ruby On Rails,Activeadmin,我有两个模型 fb_page.rb fb_页面_template.rb 主动管理员 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.rb fb_页面_template.rb 主动管理员

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_页面_模板


我只想更新
subscribed
的值,该值位于
fb\u page\u模板中

我认为您缺少以下几点:

  • 您需要在您的
    fb\u页面\u模板中允许
    accepts\u嵌套的\u属性\u用于:订阅,:allow\u destroy=>false
    。rb

  • class FbPageTemplate < ApplicationRecord
      belongs_to :fb_page
      has_many :subscriptions
      accepts_nested_attributes_for :subscriptions, :allow_destroy => false
    end
    
  • 您还需要允许ActiveAdmin中的所有嵌套属性

  • 您需要嵌套表单

这是我在我的
fb_页面.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\u页面\u模板.rb中的内容

class FbPageTemplate < ApplicationRecord
  belongs_to :fb_page
  has_many :subscriptions
  accepts_nested_attributes_for :subscriptions, :allow_destroy => false
end
class FbPageTemplatefalse
结束
希望这对你有用

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