0

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",...]}}

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.