【问题标题】:Edit relationship of parent record in activeadmin在activeadmin中编辑父记录的关系
【发布时间】:2019-09-01 06:13:51
【问题描述】:

我的模型结构如下:

  • Composition有很多ScoreScore属于Composition
  • Composition 拥有并属于许多 Countries(反之亦然)

score.rb

class Score < ApplicationRecord
  belongs_to :composition
end

composition.rb

class Composition < ApplicationRecord
  has_many :scores
  has_and_belongs_to_many :countries, join_table: :rights_countries
end

国家.rb

class Country < ApplicationRecord
  has_and_belongs_to_many :compositions, join_table: :rights_countries
end

在 activeadmin 中,我希望能够编辑作品的国家,但分数的编辑形式。

当然,表单会从作文中导入这些数据,并且默认输入对于作文的所有分数(子项)都是相等的。

到目前为止,我发现没有办法在 activeadmin 中实现这一点。

这甚至可能吗?如果是,解决方案是简单还是麻烦?

【问题讨论】:

    标签: ruby-on-rails activeadmin has-many-through has-and-belongs-to-many


    【解决方案1】:

    this link 之后,我在inputs 中添加了inputs 并更新了相应的参数。我还在分数模型中添加了accepts_nested_attributes_for :composition

    app/models/score.rb

    ...
    accepts_nested_attributes_for :composition
    ...
    

    app/admin/score.rb

    ...
    permit_params ...,
                  composition_attributes: [:id, country_ids: []]
    ...
    form do |f|
      f.inputs do
        ...
        f.inputs "", for: [:composition, score.composition] do |c|
          c.input :countries, as: :select, collection: Country.order_by_name.uniq.map { |p| [p.name, p.id] }
        end
      end
    end
    

    如果有更清洁的解决方案,请告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-23
      相关资源
      最近更新 更多