我有一个使用复选框的has_many关联,可以与statement
和identity
配合使用。
我面临的问题是如何最好地将Phase表与Identity表相关联,并仍然将其链接到语句表单。因此,对于每个statement
,可以有多个identities
,对于每个identities
,我想使用单选按钮标记关联phase
。
class Indicator < ApplicationRecord
belongs_to :statement, required: false
belongs_to :identity, required: false
belongs_to :phase, required: false
end
class Statement < ApplicationRecord
belongs_to :student, required: false
has_many :indicators
has_many :identities, :through => :indicators
accepts_nested_attributes_for :identities
end
class Identity < ApplicationRecord
has_many :indicators
has_many :statements, :through => :indicators
accepts_nested_attributes_for :statements
has_many :phases, :through => :indicators
accepts_nested_attributes_for :phases
end
class Phase < ApplicationRecord
has_many :indicators
has_many :identities, :through => :indicators
accepts_nested_attributes_for :identities
end
然后以我的形式:
<%= form_with(model: statement, local: true) do |form| %>
<table class="table table-bordered">
<% hidden_field_tag "statement[identity_ids][]", nil %>
<% Identity.all.each do |b| %>
<tr>
<td><%= check_box_tag "statement[identity_ids][]", b.id, @statement.identity_ids.include?(b.id) %></td>
<td><%= b.description %></td>
<td>CODE FOR PHASES</td>
</tr>
<% end %>
</table>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
答案 0 :(得分:0)
据我所知,has_many :phases, :through => :indicators
没有要求。 has_many :phases
应该可以正常工作。此外,当我们有方法collection_radio_buttons
和method collection_check_boxes
此外,如果您想要显示身份的相位,则应使用b.phases.all.each
代替<% Phase.all.each do |b| %>
我不确定您是否已实现此功能,并且只关注更清晰的代码。如果您遇到实际保存记录的问题,请分享错误以便更好地理解。
评论任何问题,我很乐意提供帮助。