将数据从View表单传输到Model方法

时间:2014-09-25 15:53:45

标签: html ruby-on-rails ruby model-view-controller

我正在RoR上构建一个tic-tac-toe游戏。此刻,整个电路板都已设置好,其下方有一个跟踪移动的表格。我被告知使用该模型来确定这样的事情,如果有一个胜利者(连续3个),一个领带(全板),等等。我以为我有它,但显然不是。不确定我在模型中使用players方法做错了什么。

show.html.erb

<div id="board" align = center>
  <table>
    <tr>
      <td data-position="0" class="square <%= class_for_move(0)%>"></td>
      <td data-position="1" class="square v <%= class_for_move(1)%>"></td>
      <td data-position="2" class="square <%= class_for_move(2)%>"></td>
    </tr>
    <tr>
      <td data-position="3" class="square h <%= class_for_move(3)%>"></td>
      <td data-position="4" class="square v h <%= class_for_move(4)%>"></td>
      <td data-position="5" class="square h <%= class_for_move(5)%>"></td>
    </tr>
    <tr>
      <td data-position="6" class="square <%= class_for_move(6)%>"></td>
      <td data-position="7" class="square v <%= class_for_move(7)%>"></td>
      <td data-position="8" class="square <%= class_for_move(8)%>"></td>
    </tr>
  </table>
</div>

<table>
  <tr>
    <td><%= @game.player_1 %></td>
    <td><%= @game.player_2 %></td>
  </tr>
  <tr>
    <td>X</td>
    <td>O</td>
  </tr>
</table>

<%= link_to "Play Again", games_path %>

<%= nested_form_for @game do |f| %>

  <%= f.fields_for :moves do |move_form| %>
    <div 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 %>
    </div>
  <% end %>

<input type="Submit">
<% end %>

game.rb

class Game < ActiveRecord::Base
  has_many :moves
  after_create :initialize_moves
  accepts_nested_attributes_for :moves

  def initialize_moves
    9.times do |i|
      Move.create(position: i, game:self)
    end
  end

  def players(number)
    move = moves.find_by(position: number)
    player = move.player
  end

  def tie?
  end

  def winner?
     if players(0) == players(1) && players(1) == players(2)
      return players(0)
    end
  end

end

games_controller

class GamesController < ApplicationController

  def create
    @game = Game.new(game_params)
    @game.save
    redirect_to @game
  end

  def update
    @game = Game.find(params[:id])
    @game.update(game_params)
    if @game.tie?
      flash[:error] = "Game over. 'Tis a tie."
    elsif @game.winner?
      flash[:notice] = "Winner is #{session[:current_player]}"
    else
      switch_player
    end
    redirect_to @game
  end

  def show
    @game = Game.find(params[:id])
  end

  def switch_player
    session[:current_player] = session[:current_player] == "X" ? "O" : "X"
  end

  private
  def game_params
    params.require(:game).permit(:player_1, :player_2, moves_attributes: [:player, :id])
  end

end

当我运行我目前拥有的东西时,游戏一直播放,直到我有一个三排(此时只进行0,1,2的测试作为测试),切换玩家并表现正常。在三连胜的时刻,它保持在当前播放器的任何位置,并且不会切换。它没有结束游戏,也没有给我闪光通知。只是坚持当前的球员。同样如果我尝试添加elsif试图进入另一个获胜位置组合。

我意识到这可能是初级水平的东西,但我似乎无法绕过它,到目前为止还没有找到任何有用的东西。而且,如果if陈述路线不是正确的方法,我愿意采取另一种方式。我现在无法想到一种不同的方式。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

想出来。只需要使用winner?!players(0).blank? &&方法中设置每个组合。使用players之后的每个==的相应编号。

是否会删除这个问题,但万一有人真的好奇答案,那就是。

相关问题