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