I'm figuring a problem with a nested_attribute.
team.rb:
class Team < ApplicationRecord
has_many :players, dependent: :destroy
accepts_nested_attributes_for :players, allow_destroy: true
end
console output:
Processing by TeamsController#create as JSON
Parameters: {"team"=>{"id"=>nil, "name"=>"testes",
"players_attributes"=>[{"id"=>nil, "name"=>"dadada", "_destroy"=>false, "team_id"=>nil}]}}
Unpermitted parameter: id
So, i'm ignoring team_id
in controller for create and sending it as null same to player_id
. What rails is getting in controller after permit is:
team: {name:'testes team', players_attributes: [{ name: 'testes'}]}
In my opinion (prob my mistake) rails should feed this relation in exactly this way. I tested it removing the nested attribute id
and team_id
but doesn't works.
Rails return:
bodyText: "{"players.team":["must exist"]}
controller:
def create
@team = Team.create(team_params)
@team.players.each do |player|
player.team_id = 1
end
respond_to do |format|
if @team.save
format.html { redirect_to @team, notice: 'Team was successfully created.' }
format.json { render :show, status: :created, location: @team }
else
format.html { render :new }
format.json { render json: @team.errors, status: :unprocessable_entity }
end
end
end
def team_params
params.require(:team).permit(:name, players_attributes: [:name, :positions, :_destroy])
end
gambiarra:
@team.players.each do |player|
player.team_id = 1
end
If i do this to the nested attribute BEFORE save team it works, team 1 must exists for it to work. If i save only the team and after create the relation it DOESN'T works either, only if I set the "gambiarra" solution.
How to solve this relation? As mentioned, my controller is filtering for only attributes for nested data. If i submit with HTML, works fine, if i use a JSON as nested objects, it doesn't work unless i force the relation to find a team_id
for my player before save and so on, rails will save and commit the right player as is expected to do without a team_id
in my player.