Skip to content

Instantly share code, notes, and snippets.

@oliviachang29
Last active September 29, 2018 00:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oliviachang29/0670abcd2d541fcff3f2564e9cf42636 to your computer and use it in GitHub Desktop.
Save oliviachang29/0670abcd2d541fcff3f2564e9cf42636 to your computer and use it in GitHub Desktop.
app/controllers/parents_controller.rb
class ParentsController < ApplicationController
def new
@parent = User.new
2.times {@parent.kids.build} 
# This creates a new, empty Parent instance and two empty kid instances belonging to the Parent.
end
 
 def create
  @parent = Parent.create(parent_params)
if @parent.save
redirect_to parent_path(@parent) # Only if you already have a parents/show
else
render ‘new’
end
 end
 
 private
def parent_params
  params.require(:parent).permit(:any_parent_attributes_go_here kids_attributes: [:id, :name, :_destroy]) # This permits the kids params to be saved
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment