2012-02-23 175 views
1

我試圖創建一個用於創建在同一時間遊戲和game_players一種形式。 我遇到的問題是,當我提交表單,本場比賽被創建,但game_players都沒有。嵌套形式不保存嵌套模式的Rails 3

我環顧四周,但沒有發現任何有用的答案。

博弈模型

class Game < ActiveRecord::Base 
    belongs_to :league 
    has_many :game_players, :dependent => :destroy 
    accepts_nested_attributes_for :game_players 

    attr_accessible :league_id, :game_date 

    validate :league_id, :presence => true 
end 

Game_Player型號

class GamePlayer < ActiveRecord::Base 
    belongs_to :game 
    has_many :users 

    validate :game_id, :presence => true 
    validate :user_id, :presence => true 
end 

遊戲控制器

class GamesController < ApplicationController 
    def new 
    @title = "New Game" 
    @game = Game.new 
    3.times { @game.game_players.build } 
    end 

    def create 
    @game = Game.new(:league_id => cookies[:league_id]) 
    if @game.save 
     flash[:success] = "Succesfully Created Game" 
     redirect_to League.find_by_id(cookies[:league_id]) 
    else 
     @title = "New Game" 
     render 'new' 
    end 
    end 

形式

<%= form_for @game do |f| %> 
    <%= f.fields_for :game_players do |builder| %> 
    <p> 
     <%= builder.label :user_id, "User" %><br /> 
     <%= builder.text_field :user_id %><br /> 
</p> 
    <% end %> 
    <p><%= f.submit "Submit" %></p> 
<% end %> 

回答

0

最有可能你需要傳遞:game_players_attributesattr_accessible因爲尊重。新大衆分配安全