我正在遍历构建的每个实例sub-tournament
- 我遇到的问题与有条件地创建一个collection_select
通过 ajax 获取的数据的框有关。这是视图 - 我要插入代码的行已标记:
看法
<% @tournament.sub_tournaments.each_with_index do |sub, i| %>
<%= f.fields_for :sub_tournaments, sub, :validate => false do |sub_form| %>
<div class="tab-content standings-row-<%= i %>" style="display:none">
<table>
<thead>
<tr>
<th> <h4>Standing</h4> </th>
<th class="standings-field-<%= i %>"></th>
<th></th>
</tr>
</thead>
<tbody>
<%= sub_form.fields_for :standings, :validate => false do |standings| %>
<tr>
<td>
<%= f.hidden_field :_destroy %><%= f.text_field :standing, :class => "standing", readonly: true, :type => "" %>
</td>
<td class="standings-ajax-<%= i %>">**INSERT HERE**</td>
<td><span class="remove">Remove</span></td>
</tr>
<% end %>
</tbody>
</table>
<div class="add-item">
<%= link_to_add_standings_fields(sub_form, :standings) %>
</div>
</div>
<% end %>
<% end %>
我考虑过在控制器中进行条件检查(这取决于选择的游戏是团队游戏还是单人游戏),但作为方法(或助手?)似乎更有意义。目前我在Standing.rb
(下面)中有它 - 但我遇到了一个no method "collection_select"
错误 - 所以模型中可能没有表单助手,这似乎是合理的。那么我该怎么做呢?
常设.rb
def team_or_player(game)
if Game::TEAM_GAMES.include?(game.name)
self.collection_select(:team_division_id, TeamDivision.where("game_id = ?", game.id),
:id, :name, {include_blank: true})
else
self.collection_select(:player_id, Player.where("game_id = ?", game.id),
:id, :handle, {include_blank: true})
end
end
以及如何将 传递f
给我的 AJAX 调用?
AJAX
$(".standings-ajax-<%= @tab_number %>").html("<%= ** ?? **.team_or_player(@standing, @game) %>");