我有以下模型设置。我有比赛和比赛。匹配 has_many :games,匹配接受_nested_attributes_for :games。:games_attributes 是所有成员都可以访问的。我的问题是,fields_for 没有生成正确的表单,所以 Match 不接受游戏的嵌套属性。这是我的表单代码:
<%= form_for [@tournament, match], url: tournament_match_path(@tournament, match) do |f| %>
<%= f.fields_for match.games.last do |builder| %>
<%= builder.hidden_field :winner_id, value: 1 %>
<% end %>
<%= f.submit "Win Game", class: "actionButton activeAction" %>
<% end %>
由此生成的代码是:
<form id="edit_match_1" class="edit_match" method="post" action="/tournaments/1/matches/1" accept-charset="UTF-8">
<div style="margin:0;padding:0;display:inline"> <!-- rails stuff here --></div>
<input id="match_game_winner_id" type="hidden" value="1" name="match[game][winner_id]">
<input class="actionButton activeAction" type="submit" value="Win Game" name="commit">
</form>
如您所见,match_game_winner_id 的名称不正确。名称为 match[game][winner_id],但名称应为 match[games_attributes][0][winner_id]。我怎么解决这个问题?