Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $(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)
- redirect_to @game
- switch_player
- end
- def switch_player
- session[:current_player] = session[:current_player] == 'X' ? 'O' : 'X'
- end
- <%= 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 %>
- <div id="board" align = center>
- <table>
- <tr>
- <td data-position="0" class="square <%= class_for_move(0)%>"></td>
- def class_for_move(number)
- move = @game.moves.find_by(position: number)
- player = move.player
- player.downcase + '_move' unless player.blank?
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement