我在轨道上4.如何列出要在第二个模型中选择的一个模型的所有记录?
我有一个模型features
有一个只能由我创建的记录列表。我有另一个型号houses
,用户可以创建。
我想列出所有的features
为新的house
的形式复选框,以便用户可以选择他们想要附加到他们的house
。我也有一个连接模型house_features
,将两者联系起来,因为许多houses
可以有许多不同的features
。
我house
型号:
has_many :house_features, dependent: :destroy
has_many :features, through: :house_features
accepts_nested_attributes_for :house_features
我feature
型号:
has_many :house_features
has_many :houses, through: :house_features
我house_feature
型号:
belongs_to :house
belongs_to :feature
在我的新house
形式我有
<%= form_for @house do |f| %>
<% Features.all.each do |feature| %>
<%= f.fields_for :house_features do |h| %>
<%= h.label :feature_id, feature.name %>
<%= h.check_box :feature_id, {}, feature.id %>
<% end %>
<% end %>
<% end %>
在我houses
控制器我有我的新动作@house.house_features.build
,我必须设置为'house_features_attributes的house_params:[:ID,:house_id,:FEATURE_ID]
现在,新house
页面上,该显示feature
记录复选框列表。如果我选择一些并提交表单,然后返回编辑房屋,我最终会为每个feature
列出一些额外的复选框,并且复选框的数量与我在创建时选择的功能数量相对应。
这是怎么发生的?我应该做/改变来解决这个问题吗?
我希望我的用户能够编辑他们的房屋并添加或删除功能。
感谢
感谢响应,这会使编辑页面上的多个复选框问题消失,但是,由于不会创建house_feature记录,因此不会保存任何内容。 – oobie11 2014-09-21 00:59:50