create fruits route

master
Matt Huntington 8 years ago
parent 362c4189fe
commit ac0dc7c757

@ -1,4 +1,6 @@
class FruitsController < ApplicationController
skip_before_action :verify_authenticity_token
def index
# render({ :json => { :message => 'hi', :status => 200 } })
# render json: message: 'hi', status: 200 # doesn't work because nested objects are unguessable
@ -8,4 +10,8 @@ class FruitsController < ApplicationController
def show
render json: Fruit.find(params[:id])
end
def create
render json: Fruit.create(params["fruit"])
end
end

@ -22,4 +22,9 @@ class Fruit
results = DB.exec("SELECT * FROM fruits WHERE id=#{id};")
return Fruit.new(results.first)
end
def self.create(opts={})
results = DB.exec("INSERT INTO fruits (name, color, readytoeat) VALUES ( '#{opts["name"]}', '#{opts["color"]}', #{opts["readyToEat"]} );")
return Fruit.new(opts)
end
end

@ -3,4 +3,5 @@ Rails.application.routes.draw do
get '/fruits', to: 'fruits#index'
# get('/fruits', { :to => 'fruits#index' })
get '/fruits/:id', to: 'fruits#show'
post '/fruits', to: 'fruits#create'
end

Loading…
Cancel
Save