Функция jQuery .click не работает без строки
Ссылка на первый вопрос:Ruby on Rails и Jquery: попытка переключения символов после отправки
Я задал этот вопрос вчера, и получил то, что я считаю полезным отзывом, но теперь я потерян в отношении того, что идет не так. Я пытаюсь построить игру в крестики-нолики в RoR, и в данный момент я застрял в попытке поменять игроков после отправки страницы.
JQuery:
$(document).ready(function(){
var currentPlayer = $(#table).data("current-player");
$(".square").click(function(){
// Gather the position that was clicked
var number = $(this).data("position");
// Locate the game form
var form = $("form");
// Locate the input field corresponding to that position
var input = $("input[data-position='" + number + "']");
// Set the value of that input field to "X" or "O"
input.val(currentPlayer);
// Submit the form
form.submit();
});
});
Рубин:
def update
@game = Game.find(params[:id])
@game.update(game_params)
switch_player
redirect_to @game
end
def switch_player
session[:current_player] = session[:current_player] == 'X' ? 'O' : 'X'
end
HTML-форма:
<%= nested_form_for @game do |f| %>
<%= f.fields_for :moves do |move_form| %>
<p id="table" data-current-player: <%=session[:current_player] %>>
<%= move_form.label :position %><br>
<%= move_form.text_field :player, data: {position: move_form.object.position} %>
<%= move_form.hidden_field :id %>
</p>
<% end %>
<input type="Submit">
<% end %>
HTML доска (только первая позиция):
<div id="board" align = center>
<table>
<tr>
<td data-position="0" class="square <%= class_for_move(0)%>"></td>
class_for_move:
def class_for_move(number)
move = @game.moves.find_by(position: number)
player = move.player
player.downcase + '_move' unless player.blank?
end
Сvar currentPlayer
в коде jQuery .click становится неактивным. Но с "X" (или "O", соответственно), щелчок регистрируется и отправляется правильно. Но то, как это настроено, у меня сложилось впечатление, чтоvar currentPlayer
должно быть в виде строки, которая должна позволять кликабельность.
Ссылка на следующий вопрос:jQuery click не регистрирует, какой игрок нажимает. Что мне не хватает?