adding some parent CRUD

This commit is contained in:
2015-10-03 08:04:16 -05:00
parent 9f6813c7f2
commit 8cdb204a94
10 changed files with 106 additions and 15 deletions

View File

@ -5,14 +5,40 @@ class ParentsController < ApplicationController
end
def show
@parent = Person.just_parents.where(id: params[:id])
@parent = Person.includes(:children).find_by_id(params[:id])
@more_children = Child.not_related_to(@parent.id)
end
def new
@parent = Person.new
3.times { @parent.children.build }
end
def add
ap params
redirect_to list_parents_path
@parent = Person.create add_parent_params
if @parent.persisted?
redirect_to parent_path(@parent), notice: 'Success!'
return
else
render :new
end
end
def edit
@parent = Person.includes(:children).find_by_id(params[:id])
@more_children = Child.all
end
private
def add_parent_params
params.require(:parent).permit(
:first_name,
:last_name,
:phone,
:email,
children_attributes: [:first_name, :last_name]
)
end
end

View File

@ -0,0 +1,19 @@
class RelationshipsController < ApplicationController
def add_child
parent = Person.find(params[:parent])
child = Child.find(params[:child])
parent.parenthoods.create(child)
redirect_to :back, notice: 'Child added to parent!'
end
def del_child
parent = Person.find(params[:parent])
child = Child.find(params[:child])
parent.parenthoods.find_by_child_id(child.id).destroy
redirect_to :back, notice: 'Child removed from parent!'
end
end