Last active
September 29, 2018 00:21
-
-
Save oliviachang29/0670abcd2d541fcff3f2564e9cf42636 to your computer and use it in GitHub Desktop.
app/controllers/parents_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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