Reputation: 45
I have a form that creates a league and 10 teams for that league. I am having trouble setting up the back end to do this.
Right now I have this hitting my backend:
Parameters: {"league"=>{"name"=>"League Name", "teams"=>[{"name"=>"Team 1"}, {"name"=>"Team 2"}, {"name"=>"Team 3",...]}}
Upvotes: 0
Views: 269
Reputation: 59
See if this works.
class League < ActiveRecord::Base
has_many :teams
accepts_nested_attributes_for :teams
end
And in LeagueController:league_params, whitelist the nested attributes. Note that its teams_attributes that is whitelisted and not teams
def league_params
params.require(:league).permit( :id, :name, teams_attributes: [ :id, :name ] )
end
Make sure the following param structure hits the code. Note the change from teams to teams_attributes
{"league" =>{"name"=>"League Name", "teams_attributes"=>[{"name"=>"Team 1"}, {"name"=>"Team 2"}, {"name"=>"Team 3"}]}}
The code is not tested so may need tweaking.
Upvotes: 2