From eca1b2d273a546f393f92393401cb43fc8e878cc Mon Sep 17 00:00:00 2001 From: Matt Huntington Date: Wed, 21 Mar 2018 19:32:45 -0400 Subject: [PATCH] Add a person to a location --- app/controllers/people_controller.rb | 5 +++++ app/models/person.rb | 5 ++++- config/routes.rb | 3 +++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/app/controllers/people_controller.rb b/app/controllers/people_controller.rb index cded508..7eadd1e 100644 --- a/app/controllers/people_controller.rb +++ b/app/controllers/people_controller.rb @@ -10,6 +10,11 @@ class PeopleController < ApplicationController end def create + if params["id"] + params["person"]["home_id"] = params["id"].to_i + else + params["person"]["home_id"] = "NULL" + end render json: Person.create(params["person"]) end diff --git a/app/models/person.rb b/app/models/person.rb index 6d9270b..f0b4c40 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -6,6 +6,9 @@ class Person @id = opts["id"].to_i @name = opts["name"] @age = opts["age"].to_i + if opts["home_id"] + @home_id = opts["home_id"] + end end def self.all @@ -19,7 +22,7 @@ class Person end def self.create(opts={}) - results = DB.exec("INSERT INTO people (name, age) VALUES ( '#{opts["name"]}', #{opts["age"]} );") + results = DB.exec("INSERT INTO people (name, age, home_id) VALUES ( '#{opts["name"]}', #{opts["age"]}, #{opts["home_id"]} );") return { created:true } end diff --git a/config/routes.rb b/config/routes.rb index 7d7aba0..d03d1bc 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -17,4 +17,7 @@ Rails.application.routes.draw do post '/companies', to: 'companies#create' delete '/companies/:id', to: 'companies#delete' put '/companies/:id', to: 'companies#update' + + post '/locations/:id/people', to: 'people#create' + end