From 7afe8d137cd4952bfaac412bd4bf4606d5d3b444 Mon Sep 17 00:00:00 2001 From: Mark Moser Date: Sat, 3 Oct 2015 19:16:46 -0500 Subject: [PATCH] parent/child CRUD complete --- app/controllers/children_controller.rb | 40 +++++++++++++++++++++++++- app/controllers/docs_controller.rb | 4 +-- app/controllers/parents_controller.rb | 14 +++++++-- app/models/person.rb | 2 +- app/views/children/_form.html.haml | 14 +++++++++ app/views/children/_sub_nav.html.haml | 3 +- app/views/children/edit.html.haml | 5 ++++ app/views/children/index.html.haml | 8 ++++-- app/views/children/new.html.haml | 3 ++ app/views/children/show.html.haml | 18 ++++++++++++ app/views/docs/index.html.haml | 2 -- app/views/parents/edit.html.haml | 24 ++++++++++++++++ app/views/parents/index.html.haml | 8 +++--- app/views/parents/show.html.haml | 2 +- app/views/staff/_sub_nav.html.haml | 3 +- app/views/users/_sub_nav.html.haml | 3 +- config/routes.rb | 17 +++++++---- 17 files changed, 144 insertions(+), 26 deletions(-) create mode 100644 app/views/children/_form.html.haml create mode 100644 app/views/children/edit.html.haml create mode 100644 app/views/children/new.html.haml create mode 100644 app/views/children/show.html.haml create mode 100644 app/views/parents/edit.html.haml diff --git a/app/controllers/children_controller.rb b/app/controllers/children_controller.rb index 0195aa0..cc642c5 100644 --- a/app/controllers/children_controller.rb +++ b/app/controllers/children_controller.rb @@ -4,5 +4,43 @@ class ChildrenController < ApplicationController respond_with @children end - def lookup; end + def show + @child = Child.includes(:parents).find_by_id(params[:id]) + @parents = Person.order(:first_name, :last_name) + end + + def new + @child = Child.new + end + + def edit + @child = Child.find_by_id(params[:id]) + end + + def add + @child = Child.create child_params + if @child.persisted? + redirect_to child_path(@child), notice: 'Created!' + else + render :new + end + end + + def update + @child = Child.find_by_id params[:id] + if @child.update child_params + redirect_to child_path(@child), notice: 'Updated!' + else + render :edit + end + end + + private + + def child_params + params.require(:child).permit( + :first_name, + :last_name + ) + end end diff --git a/app/controllers/docs_controller.rb b/app/controllers/docs_controller.rb index 7d77aef..cb686c8 100644 --- a/app/controllers/docs_controller.rb +++ b/app/controllers/docs_controller.rb @@ -1,8 +1,8 @@ class DocsController < ApplicationController def index @doc = { - name: "sms-pager-api", - documentation: "https://bitbucket.org/markamoser/sms-pager-api" + name: "sms-pager", + documentation: "https://bitbucket.org/markamoser/sms-pager" } respond_with @doc end diff --git a/app/controllers/parents_controller.rb b/app/controllers/parents_controller.rb index 573c3f3..765c800 100644 --- a/app/controllers/parents_controller.rb +++ b/app/controllers/parents_controller.rb @@ -15,7 +15,7 @@ class ParentsController < ApplicationController end def add - @parent = Person.create add_parent_params + @parent = Person.create parent_params if @parent.persisted? redirect_to parent_path(@parent), notice: 'Success!' @@ -30,9 +30,19 @@ class ParentsController < ApplicationController @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 add_parent_params + def parent_params params.require(:parent).permit( :first_name, :last_name, diff --git a/app/models/person.rb b/app/models/person.rb index 65e08a4..07624de 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -13,7 +13,7 @@ class Person < ActiveRecord::Base scope :just_parents, lambda { joins(:children) - .order(:first_name) + .order(:first_name, :last_name) .uniq } diff --git a/app/views/children/_form.html.haml b/app/views/children/_form.html.haml new file mode 100644 index 0000000..ba8e1ff --- /dev/null +++ b/app/views/children/_form.html.haml @@ -0,0 +1,14 @@ +- if @child.errors.full_messages.any? + .errors + %h1 Uh oh! + - @child.errors.full_messages.each do |error| + %p= error + += form_for @child, as: :child, url: add_child_path do |f| + = f.label :first_name + = f.text_field :first_name + + = f.label :last_name + = f.text_field :last_name + + = f.submit diff --git a/app/views/children/_sub_nav.html.haml b/app/views/children/_sub_nav.html.haml index 0942d83..215428e 100644 --- a/app/views/children/_sub_nav.html.haml +++ b/app/views/children/_sub_nav.html.haml @@ -1,3 +1,4 @@ %ul.sub-nav - %li= link_to 'look up', lookup_child_path + %li= link_to 'List', list_children_path + %li= link_to 'New', new_child_path diff --git a/app/views/children/edit.html.haml b/app/views/children/edit.html.haml new file mode 100644 index 0000000..855edd0 --- /dev/null +++ b/app/views/children/edit.html.haml @@ -0,0 +1,5 @@ +%h2 Edit #{@child.name} + +%p= link_to 'back', child_path(@child) + += render partial: 'form' diff --git a/app/views/children/index.html.haml b/app/views/children/index.html.haml index 1dd0ccd..dae2f11 100644 --- a/app/views/children/index.html.haml +++ b/app/views/children/index.html.haml @@ -2,7 +2,11 @@ - @children.each do |child| %ul - %li= child.name + %li + = link_to child.name, child_path(child) + = link_to 'edit', edit_child_path(child) %ul - child.parents.each do |parent| - %li #{parent.name} #{page_link(parent)} + %li + = link_to parent.name, parent_path(parent) + = page_link(parent) diff --git a/app/views/children/new.html.haml b/app/views/children/new.html.haml new file mode 100644 index 0000000..7874876 --- /dev/null +++ b/app/views/children/new.html.haml @@ -0,0 +1,3 @@ +%h2 Add a New Child + += render partial: 'form' diff --git a/app/views/children/show.html.haml b/app/views/children/show.html.haml new file mode 100644 index 0000000..9ef8589 --- /dev/null +++ b/app/views/children/show.html.haml @@ -0,0 +1,18 @@ +%h2= @child.name +%p= link_to 'edit', edit_child_path(@child) + +- unless @child.parents.empty? + %p Parents: + %ul + - @child.parents.each do |parent| + %li + = link_to parent.name, parent_path(parent) + = link_to 'remove', del_parenthood_path(parent, @child), method: :delete + +%p Add Parent: += form_tag add_parent_to_child_path(@child) do + :ruby + select_options = options_from_collection_for_select(@parents, :id, :name) + html_options = { include_blank: true } + = select_tag(:parent, select_options, html_options) + = submit_tag 'Add Parent to Child' diff --git a/app/views/docs/index.html.haml b/app/views/docs/index.html.haml index 8052372..c5b419d 100644 --- a/app/views/docs/index.html.haml +++ b/app/views/docs/index.html.haml @@ -1,4 +1,2 @@ %h2 Something helpful later - %p= raw(ap @doc) - diff --git a/app/views/parents/edit.html.haml b/app/views/parents/edit.html.haml new file mode 100644 index 0000000..bf57a62 --- /dev/null +++ b/app/views/parents/edit.html.haml @@ -0,0 +1,24 @@ +%h2 Edit #{@parent.name} + +%p= link_to 'back', parent_path(@parent) + +- if @parent.errors.full_messages.any? + .errors + %h1 Uh oh! + - @parent.errors.full_messages.each do |e| + %p= e + += form_for @parent, as: :parent, url: update_parent_path do |f| + = f.label :first_name + = f.text_field :first_name + + = f.label :last_name + = f.text_field :last_name + + = f.label :phone + = f.phone_field :phone + + = f.label :email + = f.email_field :email + + = f.submit diff --git a/app/views/parents/index.html.haml b/app/views/parents/index.html.haml index a7af5d2..595add0 100644 --- a/app/views/parents/index.html.haml +++ b/app/views/parents/index.html.haml @@ -2,12 +2,12 @@ - @parents.each do |parent| %ul + %li= link_to parent.name, parent_path(parent) %li - = link_to parent.name, parent_path(parent) + = number_to_phone parent.phone = page_link(parent) - %li= number_to_phone parent.phone - %li= mail_to parent.email + %li= mail_to parent.email, nil, encode: 'hex' %li Children: %ul - parent.children.each do |child| - %li= child.name + %li= link_to child.name, child_path(child) diff --git a/app/views/parents/show.html.haml b/app/views/parents/show.html.haml index 0319f6d..30db3a0 100644 --- a/app/views/parents/show.html.haml +++ b/app/views/parents/show.html.haml @@ -8,7 +8,7 @@ %ul - @parent.children.each do |child| %li - = child.name + = link_to child.name, child_path(child) = link_to 'remove', del_parenthood_path(@parent, child), method: :delete %p Add Child: diff --git a/app/views/staff/_sub_nav.html.haml b/app/views/staff/_sub_nav.html.haml index 4ce1937..55e946f 100644 --- a/app/views/staff/_sub_nav.html.haml +++ b/app/views/staff/_sub_nav.html.haml @@ -1,2 +1 @@ -%ul.sub-nav - +-# %ul.sub-nav diff --git a/app/views/users/_sub_nav.html.haml b/app/views/users/_sub_nav.html.haml index 4ce1937..55e946f 100644 --- a/app/views/users/_sub_nav.html.haml +++ b/app/views/users/_sub_nav.html.haml @@ -1,2 +1 @@ -%ul.sub-nav - +-# %ul.sub-nav diff --git a/config/routes.rb b/config/routes.rb index 9ba9f83..1fcd5ea 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,16 +1,21 @@ Rails.application.routes.draw do get 'parents', to: 'parents#index', as: :list_parents - get 'parents/new', to: 'parents#new', as: :new_parent - post 'parents/new', to: 'parents#add', as: :add_parent - get 'parents/edit/:id', to: 'parents#edit', as: :edit_parent - get 'parents/:id', to: 'parents#show', as: :parent - post 'parents/:id', to: 'parents#update', as: :update_parent + get 'parent/new', to: 'parents#new', as: :new_parent + post 'parent/new', to: 'parents#add', as: :add_parent + get 'parent/edit/:id', to: 'parents#edit', as: :edit_parent + get 'parent/:id', to: 'parents#show', as: :parent + patch 'parent/:id', to: 'parents#update', as: :update_parent + post 'parenthood/child/:child', to: 'relationships#add_child', as: :add_parent_to_child post 'parenthood/:parent/', to: 'relationships#add_child', as: :add_parenthood delete 'parenthood/:parent/:child', to: 'relationships#del_child', as: :del_parenthood get 'children', to: 'children#index', as: :list_children - get 'children/lookup', to: 'children#lookup', as: :lookup_child + get 'child/new', to: 'children#new', as: :new_child + post 'child/new', to: 'children#add', as: :add_child + get 'childdit/:id', to: 'children#edit', as: :edit_child + get 'child/:id', to: 'children#show', as: :child + patch 'child/:id', to: 'children#update', as: :update_child get 'staff', to: 'staff#index', as: :list_staff