【发布时间】: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