From 11435c3661133ef6ac6bb48ca5ca6e4694cbfb0c Mon Sep 17 00:00:00 2001 From: Matt Huntington Date: Tue, 20 Mar 2018 21:24:02 -0400 Subject: [PATCH] Create a create route --- app/controllers/people_controller.rb | 6 ++++++ app/models/person.rb | 5 +++++ config/routes.rb | 1 + 3 files changed, 12 insertions(+) diff --git a/app/controllers/people_controller.rb b/app/controllers/people_controller.rb index 5709c1d..1257d0e 100644 --- a/app/controllers/people_controller.rb +++ b/app/controllers/people_controller.rb @@ -1,4 +1,6 @@ class PeopleController < ApplicationController + skip_before_action :verify_authenticity_token + def index render json: Person.all end @@ -6,4 +8,8 @@ class PeopleController < ApplicationController def show render json: Person.find(params[:id]) end + + def create + render json: Person.create(params["person"]) + end end diff --git a/app/models/person.rb b/app/models/person.rb index 50f450c..ab5669f 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -17,4 +17,9 @@ class Person results = DB.exec("SELECT * FROM people WHERE id=#{id};") return Person.new(results.first) end + + def self.create(opts={}) + results = DB.exec("INSERT INTO people (name, age) VALUES ( '#{opts["name"]}', #{opts["age"]} );") + return { created:true } + end end diff --git a/config/routes.rb b/config/routes.rb index b09e193..d93537b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,4 +2,5 @@ Rails.application.routes.draw do # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html get '/people', to: 'people#index' get '/people/:id', to: 'people#show' + post '/people', to: 'people#create' end