多个嵌套表单 - 多个属于

时间:2011-02-28 16:38:20

标签: ruby-on-rails ruby ruby-on-rails-3 forms

我有一个球员,比赛,投篮,轮和团队模型。 创建镜头时,它属于所有这些模型。 (这是因为球队并不总是拥有相同的球员,但原因并不重要。) 我的游戏形式接受了_sted_attributes for rounds,我的回合接受了_sted_attributes_for我的投篮(每支队伍总共有6-3个)

总结一下,我有一个嵌套的游戏形式=> rounds =>我需要镜头才能拥有round_id,player_id,team_id和game_id。

我应该使用f.hidden_​​field,如果是,那不是很危险吗?即使我如此,我如何连线以使ids在正确的位置?

我发布了以下观点,如果需要其他任何内容请告诉我。

发/ _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 %>

发/ _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>

发/ _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>

1 个答案:

答案 0 :(得分:-1)

也许可以使用指向所需变量的会话变量。