【问题标题】:Rails active admin for a has_many through association通过关联的 has_many 的 Rails 活动管理员
【发布时间】:2012-03-13 15:30:21
【问题描述】:

如何在 active_admin 表单中显示连接模型的额外属性(在 has_many through 关联中)?

f.input :ad_slots

将只显示 ad_slots 模型,但我有一个名为 stream_slots 的连接表,其中有一个名为 duration 的额外列。我希望能够选择 ad_slot 以及输入持续时间。

这些是模型:

#live_stream:
has_many :stream_slots, :dependent => :destroy
has_many :ad_slots, :through => :stream_slots

#ad_slot:
has_many :stream_slots, :dependent => :destroy
has_many :live_streams, :through => :stream_slots

#stream_slot:
belongs_to :ad_slot
belongs_to :live_stream

并且 stream_slot 具有其他两个模型的 id 以及一个称为持续时间的额外属性。

谢谢。

-- 尝试了其他方法--

我没有链接到 ad_slots,而是这样做:

f.has_many :stream_slots do |association|
  association.inputs do 
    association.input :ad_slot
    association.input :duration
  end

结束

在 LiveStream 课程中,我添加了:

accepts_nested_attributes_for :stream_slots

这样,表单会显示 ad_slots 供我选择,以及额外属性的文本字段,但是,当我尝试保存它时它失败了。我相信我知道问题是什么,但我不知道如何解决它。这是因为 stream_slots 表中的 live_stream_id 是空的。如何将其设置为新创建的 live_stream? (我现在正在创建的那个..)

任何帮助将不胜感激..

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 forms has-many-through activeadmin


    【解决方案1】:

    我搞定了,正如问题中提到的那样,问题是提交表单时,live_stream_id 为空。

    所以我刚刚删除了 stream_slot 模型中的验证,该验证表明 live_stream_id 必须存在并且有效。

    不确定这是否是最好的方法。但它现在正在工作。

    如果有人有更好的解决方案,请分享。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-27
      相关资源
      最近更新 更多