From fe75d12839830b33fb4be42d9a31cbf1bb2f30bb Mon Sep 17 00:00:00 2001 From: Matt Huntington Date: Thu, 15 Mar 2018 20:15:26 -0400 Subject: [PATCH] fruit show route --- app/controllers/fruits_controller.rb | 4 ++++ app/models/fruit.rb | 5 +++++ config/routes.rb | 1 + 3 files changed, 10 insertions(+) diff --git a/app/controllers/fruits_controller.rb b/app/controllers/fruits_controller.rb index b538777..cdb7008 100644 --- a/app/controllers/fruits_controller.rb +++ b/app/controllers/fruits_controller.rb @@ -4,4 +4,8 @@ class FruitsController < ApplicationController # render json: message: 'hi', status: 200 # doesn't work because nested objects are unguessable render json: Fruit.all end + + def show + render json: Fruit.find(params[:id]) + end end diff --git a/app/models/fruit.rb b/app/models/fruit.rb index 389c6ad..995139a 100644 --- a/app/models/fruit.rb +++ b/app/models/fruit.rb @@ -12,4 +12,9 @@ class Fruit results = DB.exec("SELECT * FROM fruits;") return results.map { |fruit_opts| Fruit.new(fruit_opts)} end + + def self.find(id) + results = DB.exec("SELECT * FROM fruits WHERE id=#{id};") + return Fruit.new(results.first) + end end diff --git a/config/routes.rb b/config/routes.rb index 144c764..da1a659 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 '/fruits', to: 'fruits#index' # get('/fruits', { :to => 'fruits#index' }) + get '/fruits/:id', to: 'fruits#show' end