From 424ee9b47315a447bd2174e06bf72240660e6261 Mon Sep 17 00:00:00 2001 From: Matt Huntington Date: Tue, 20 Mar 2018 21:05:21 -0400 Subject: [PATCH] Create a show route --- app/controllers/people_controller.rb | 4 ++++ app/models/person.rb | 5 +++++ config/routes.rb | 1 + 3 files changed, 10 insertions(+) diff --git a/app/controllers/people_controller.rb b/app/controllers/people_controller.rb index 3585a4c..5709c1d 100644 --- a/app/controllers/people_controller.rb +++ b/app/controllers/people_controller.rb @@ -2,4 +2,8 @@ class PeopleController < ApplicationController def index render json: Person.all end + + def show + render json: Person.find(params[:id]) + end end diff --git a/app/models/person.rb b/app/models/person.rb index 75ffe6f..50f450c 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -12,4 +12,9 @@ class Person results = DB.exec("SELECT * FROM people;") return results.map { |result| Person.new(result) } end + + def self.find(id) + results = DB.exec("SELECT * FROM people WHERE id=#{id};") + return Person.new(results.first) + end end diff --git a/config/routes.rb b/config/routes.rb index e1ec75e..b09e193 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,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' end