sms-pager/app/controllers/parents_controller.rb

55 lines
1.0 KiB
Ruby

class ParentsController < ApplicationController
def index
@parents = Person.just_parents
respond_with @parents
end
def show
@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
@parent = Person.create 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
def update
@parent = Person.find(params[:id])
if @parent.update(parent_params)
redirect_to parent_path(@parent), notice: 'Success!'
else
render :edit
end
end
private
def parent_params
params.require(:parent).permit(
:first_name,
:last_name,
:phone,
:email,
children_attributes: [:first_name, :last_name]
)
end
end