fruit show route

master
Matt Huntington 8 years ago
parent d5558fc2ee
commit fe75d12839

@ -4,4 +4,8 @@ class FruitsController < ApplicationController
# render json: message: 'hi', status: 200 # doesn't work because nested objects are unguessable # render json: message: 'hi', status: 200 # doesn't work because nested objects are unguessable
render json: Fruit.all render json: Fruit.all
end end
def show
render json: Fruit.find(params[:id])
end
end end

@ -12,4 +12,9 @@ class Fruit
results = DB.exec("SELECT * FROM fruits;") results = DB.exec("SELECT * FROM fruits;")
return results.map { |fruit_opts| Fruit.new(fruit_opts)} return results.map { |fruit_opts| Fruit.new(fruit_opts)}
end end
def self.find(id)
results = DB.exec("SELECT * FROM fruits WHERE id=#{id};")
return Fruit.new(results.first)
end
end end

@ -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 # 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', { :to => 'fruits#index' }) # get('/fruits', { :to => 'fruits#index' })
get '/fruits/:id', to: 'fruits#show'
end end

Loading…
Cancel
Save