在控制器中保存模型关联

时间:2010-10-05 14:29:57

标签: ruby-on-rails

我设置了以下型号:

class Player < ActiveRecord::Base
  has_many :game_players
  has_many :games, :through => :game_players
end

class Game < ActiveRecord::Base
  has_many :game_players
  has_many :players, :through => :game_players 
end

class GamePlayer < ActiveRecord::Base
  belongs_to :game
  belongs_to :player
end

每场比赛有四名球员。我需要在控制器中为保存新游戏提供一些指导,而GamePlayer模型中的4个玩家看起来就像。到目前为止,这就是我在控制器中所拥有的:

  # POST /games
  # POST /games.xml
  def create
    @game = Game.new(params[:game])

    respond_to do |format|
      if @game.save

        format.html { redirect_to(@game, :notice => 'Game was successfully created.') }
        format.xml  { render :xml => @game, :status => :created, :location => @game }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @game.errors, :status => :unprocessable_entity }
      end
    end
  end

显然它只保存游戏模型,但我不清楚如何在GamePlayer中创建和保存4个玩家。 GamePlayer真的应该有一个game_id和player_id,每行有一些额外的元数据(比如谁扮演防守等)。

任何帮助都将不胜感激。

在我的视图中,我有以下内容:

<%= form_for(@game) do |f| %>
  <% if @game.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@game.errors.count, "error") %> prohibited this game from being saved:</h2>

      <ul>
      <% @game.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :blue_score %> :
    <%= f.text_field :blue_score %><br />
    <%= f.label :white_score %> :
    <%= f.text_field :white_score %><br />


    <%= label_tag(:white_offense, "White Offense:") %>
    <%= select_tag "white_offense", options_from_collection_for_select(@players, "name", "id")
    <%= select_tag "white_defense", options_from_collection_for_select(@players, "name", "id")
    <%= select_tag "blue_offense", options_from_collection_for_select(@players, "name", "id")
    <%= select_tag "blue_defense", options_from_collection_for_select(@players, "name", "id")


    <%= submit_tag("New Game") %>


  </div>
<% end %>

1 个答案:

答案 0 :(得分:0)

虽然我觉得比这更好(可能使用Collections?),但这是我的解决方案:

<%= label_tag(:white_offense, "White Offense:") %>
<%= select_tag "white_offense", options_from_collection_for_select(@players, "id", "name") %>
<br />
<%= label_tag(:white_defense, "White Defense:") %>
<%= select_tag "white_defense", options_from_collection_for_select(@players, "id", "name") %>
<br />
<%= label_tag(:blue_offense, "Blue Offense:") %>
<%= select_tag "blue_offense", options_from_collection_for_select(@players, "id", "name") %>
<br />
<%= label_tag(:blue_defense, "Blue Defense:") %>
<%= select_tag "blue_defense", options_from_collection_for_select(@players, "id", "name") %>
<br />
<%= submit_tag("New Game") %>

我的控制员:

@game = Game.new(params[:game])
@game.game_players.build(:player_id => params[:white_offense])
@game.game_players.build(:player_id => params[:white_defense], :is_defender => 1)
@game.game_players.build(:player_id => params[:blue_offense], :is_blue => 1)
@game.game_players.build(:player_id => params[:blue_defense], :is_blue => 1, :is_defender => 1)
相关问题