繁体   English   中英

通过关联对has_many进行Rails活动管理员

[英]Rails active admin for a has_many through association

如何以active_admin形式显示联接模型的额外属性(通过has_many通过关联)?

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以及一个称为duration的额外属性。

谢谢。

-尝试了其他东西-

这是我所做的,而不是链接到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供我选择,以及文本框中的extra属性,但是,当我尝试保存它时会失败。 我相信我知道问题出在哪里,但我不知道如何解决。 这是因为stream_slots表中的live_stream_id为空。 如何将其设置为新创建的live_stream? (我现在正在创建的那个。)

任何帮助,将不胜感激..

我得到了它的支持,正如问题中提到的那样,问题是提交表单时,live_stream_id为空。

因此,我只是删除了stream_slot模型中的验证,该验证中指出live_stream_id必须存在并且有效。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM