I have a player, game, shot, round and team model.
When creating a shot, it belongs_to a all of those models. (This is because a team does not always have the same players, but reasons are unimportant.)
My game form accepts_nested_attributes for rounds and my rounds accepts_nested_attributes_for my shots (of which there are always 6 — 3 on each team)
To summarize, I have a nested form for game => rounds => shots and I need shots to have a round_id, player_id, team_id and game_id.
Should I use f.hidden_field, and if so, isn’t that dangerous? Even if I so how do I wire it so that the ids are in the right place?
I posted the views below, if anything else is needed let me know.
rounds/_form.html.erb
<% if @round.errors.any? %>
<div class="error">
<% @round.errors.full_messages.each do |msg| %>
<%= msg %><br/>
<% end %>
</div>
<% end %>
<%= form_for @game do |f| %>
<%= field_set_tag "#{@game.away.name} at #{@game.home.name}" do %>
<table class="sortable">
<thead>
<tr>
<th>Number</th>
<th><%= @game.away_players[0].name %></th>
<th><%= @game.away_players[1].name %></th>
<th><%= @game.away_players[2].name %></th>
<th><%= @game.home_players[0].name %></th>
<th><%= @game.home_players[1].name %></th>
<th><%= @game.home_players[2].name %></th>
<th>Remove</th>
</tr>
</thead>
<tbody>
<%= f.fields_for :rounds do |round_form| %>
<%= render 'round_fields', :f => round_form %>
<% end -%>
</tbody>
</table>
<p>
<%= link_to_add_fields "Add Round", f, :rounds %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
<% end %>
rounds/_round_fields.html.erb
<% 6.times { f.object.shots.build } if f.object.new_record? -%>
<tr>
<td>
<%= f.text_field :number, :size => 3 %>
</td>
<%= f.fields_for :shots do |shot_form| %>
<%= render 'shot_fields', :f => shot_form %>
<% end -%>
<td>
<%= f.check_box(:_destroy) %>
<%= f.hidden_field :id %>
</td>
</tr>
rounds/_shot_fields.html.erb
<td>
<%= f.select :cup, [["Miss", 0], 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ["Suicide", 11]], :include_blank => "No Shot" %>
<%# f.hidden_field :id %>
</td>
Maybe session vars that point to the required variables could work.