具有has_many关系的ActiveAdmin模型 - 自定义表单按钮到“添加所有”可能的资源

时间:2018-02-14 18:05:22

标签: ruby-on-rails activeadmin

示例模型:

class Person
  has_many :auto_mappings, dependent: :destroy   
class Vehicle
  has_many :auto_mappings
class AutoMapping
  belongs_to :person
  belongs_to :vehicle

  validates :person,
            :vehicle,
            :usage,
            presence: true

Person上的“创建/编辑”的ActiveAdmin表单中,我为嵌套的f.has_many资源添加了一个输入AutoMapping,以便一次添加一个AutoMapping,效果很好。我想要完成的是以某种方式添加一个按钮,为每个AutoMapping Vehicle添加Person

有没有办法实现这个目标还是我应该采取另一个方向?

Person的示例AA表单:

form do |f|
  f.inputs 'Details' do
    f.input :name
    f.input :age
    f.has_many :auto_mappings, class: 'auto_mapping' do |item|
      item.input :vehicle, include_blank: false, input_html: {class: "vehicle", disabled: item.object.persisted?}
      item.input :usage,
                 as: :select,
                 collection: %w[drive ride both]
                 include_blank: false
    end
  end
  # Maybe a custom member action to "Create Person with all Auto Mappings"?
  # I would prefer to auto populate the form page with `AutoMappings` rather than create them all on Submit if possible.
  f.actions
end

谢谢!

1 个答案:

答案 0 :(得分:0)

您必须扩展has_many.es6才能进行Ajax调用并添加项目。