我无法为我的项目创建多对多模型。
基本上我有一个 Matches & Teams 模型。
球队是在比赛之前创建的。
创建比赛后,我想向其中添加球队。
比赛可以有很多球队,球队可以有很多比赛。
我目前正在通过nested_form 添加团队并一次添加多个团队。
提交表格时,我收到一个错误,期望球队已经与比赛建立关系。
我可以通过多对一的关系来做到这一点,但在多对多的情况下它会失败,想知道是否有任何方法可以在不使用自定义路由的情况下做到这一点。
下面是表格,控制器按照默认值。
形式:
<%= nested_form_for(@match) do |f| %>
<% if @match.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@match.errors.count, "error") %> prohibited this match from being saved:</h2>
<ul>
<% @match.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :date %><br />
<%= f.date_select :date %>
</div>
<%= f.fields_for :teams, :html => { :class => 'form-vertical' } do |builder| %>
<%= builder.label "Team Name:" %>
<%= builder.autocomplete_field :name, autocomplete_team_name_teams_path, :update_elements => {:id => "##{form_tag_id(builder.object_name, :id)}" },:class => "input-small",:placeholder => "Search" %>
<%= builder.hidden_field :id %>
<% end %>
<%= f.link_to_add raw('<i class="icon-plus-sign"></i>'), :teams, :class => 'btn btn-small btn-primary' %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>