I have the following code in my view:
<% @games.each_with_index do |game, i| %>
<div class="game_<%= i %> game_hide hide" >
<% options = options_from_collection_for_select(Player.where("game_id = ?", game.id), 'id', 'handle' ) %>
<%= f.select(:player_ids, options, {:include_blank => true}, {id: "game_#{i}", :multiple => true} ) %>
</div>
<% end %>
Which generates the following html:
<div class="add-players">
<div class="game_0 game_hide hide" >
<input name="team[team_divisions_attributes][0][player_ids][]" type="hidden" value="" />
<select id="game_0" multiple="multiple" name="team[team_divisions_attributes][0][player_ids][]"><option value=""></option>
<option value="2551">Näryt</option>
<option value="2552">BrTT</option>
<option value="2553">Danagorn</option>
...
</select>
</div>
<div class="game_1 game_hide hide" >
<input name="team[team_divisions_attributes][0][player_ids][]" type="hidden" value="" />
<select id="game_1" multiple="multiple" name="team[team_divisions_attributes][0][player_ids][]"><option value=""></option>
<option value="4885">Zium</option>
<option value="4886">Abver</option>
<option value="4887">Xenocider</option>
...
</select>
</div>
<div class="game_2 game_hide hide" >
<input name="team[team_divisions_attributes][0][player_ids][]" type="hidden" value="" />
<select id="game_2" multiple="multiple" name="team[team_divisions_attributes][0][player_ids][]"><option value=""></option>
<option value="4865">Odin</option>
<option value="4866">Nazgul</option>
<option value="4867">Dragon</option>
...
</select>
</div>
</div>
and is placed with the following jQuery:
//when box is checked...
$('.show-tabs .show-tab').live('click', function(){
var tab = $('.tabs .tab:nth-of-type(' + ( $(this).index() + 1 ) + ')');
var content = $('.tab-content:nth-of-type(' + ( $(this).index() + 1 ) + ')');
var destroy_field = content.find('input#team_team_divisions_attributes_' + $(this).index() +'__destroy');
if ($(this).find('.checkbox').hasClass('checkBoxed')){
//make it visible
destroy_field.val('0');
//shows the game div for the selected game
$(".game-"+ $(this).index()).show();
//what's the index?
alert($(this).index());
//show the associated tab
tab.show();
tab.click();
} else {
destroy_field.val('1');
tab.hide();
if (tab.hasClass('selected')) {
$('.tabs .tab:visible').first().click();
content.hide();
}
}
} );
Everything is "there" - in that I can find it on the page with developer tools in Chrome, but the select box is only being displayed for the first game (game_0
) - Because of this, I think there's some sort of issue with the id
's but I can't figure out what it is.