adding some parent CRUD
This commit is contained in:
@ -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
|
||||
|
19
app/controllers/relationships_controller.rb
Normal file
19
app/controllers/relationships_controller.rb
Normal 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
|
Reference in New Issue
Block a user