简体   繁体   中英

Nested form doesn't save because parent doesn't exist

I have three classes: Players, Teams and Games .

Players play on a team and each game has multiple teams playing against each other.

I have a nested form which is supposed to create players, teams and games in one go, using strong parameters. This works if I'm only creating the game or even when I'm creating the game and team. However, once the players are added to the mix, the entire thing fails with ActiveRecord::RecordInvalid (Validation failed: Players team must exist).

Any thoughts would be most appreciated.

My Models:

class Game < ApplicationRecord
    has_many :teams
    has_many :players, through: :teams
    accepts_nested_attributes_for :teams, :players
end

class Team < ApplicationRecord
    belongs_to :game
    has_many :players
    accepts_nested_attributes_for :players
end

class Player < ApplicationRecord
    belongs_to :team
end

My GamesController :

class GamesController < ApplicationController

def new
        @game = Game.new
        @team = @game.teams.build
        @player = @game.players.build
    end

    def create  
        @game = Game.new(game_params)
        if @game.save
            flash[:success] = "Booking successful!"
            redirect_to root_path
        else
            render 'new'
        end
    end

    private

    def game_params
        params.require(:game).permit(:start_time, teams_attributes: [:name], players_attributes: [:first_name])
    end
end

And finally, my form partial:

<%= form_with model: @game do |f| %>

    <%= f.fields_for :players do |f_players| %>
        <%= f_players.label :first_name %>
        <%= f_players.text_field :first_name %>
    <% end %>

    <%= f.fields_for :teams do |f_teams| %>
        <%= f_teams.label :name, "Team name" %>
        <%= f_teams.text_field :name %>
    <% end %>

    <%= f.label :start_time, "Game date" %>
    <%= f.date_field :start_time %>

    <%= f.submit "Confirm" %>
<% end %>

You're trying to build multiple teams with each team having multiple players. However, you're trying to create all the players at the same level, so rails doesn't know which team they should go in.

If we were to do this in a longhand way, we'd create the team and list players within it.

<%= form_with model: @game do |f| %>

    <%= f.fields_for :teams do |f_teams| %>
        <%= f_teams.label :name, "Team name" %>
        <%= f_teams.text_field :name %>

      <%= f_teams.fields_for :players do |f_players| %>
        <%= f_players.label :first_name %>
        <%= f_players.text_field :first_name %>
      <% end %>
    <% end %>

    <%= f.label :start_time, "Game date" %>
    <%= f.date_field :start_time %>

    <%= f.submit "Confirm" %>
<% end %>

The problem with this is that both teams and players need to allow 1+ records. To do this you'd need to use JS to add a new team or a new player. The team that they are connected to must be preserved and validated.

You may find these articles helpful:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM