Skip to content

Instantly share code, notes, and snippets.

@ascsystems
Last active December 31, 2015 04:49
Show Gist options
  • Select an option

  • Save ascsystems/7936921 to your computer and use it in GitHub Desktop.

Select an option

Save ascsystems/7936921 to your computer and use it in GitHub Desktop.
class AnnotationsController < ApplicationController
def index
annotations = Annotation.all
render json: annotations
end
def show
annotation = Annotation.find(params[:id])
render json: annotation
end
def new
annotation = Annotation.new
render json: annotation
end
def edit
annotation = Annotation.find(params[:id])
end
def create
annotation = Annotation.create(note_params)
if annotation.save
render json: annotation, status: :created
else
render json: annotation.errors, status: :unprocessable_entity
end
end
def update
annotation = Annotation.find(params[:id])
if annotation.update_attributes(params)
head :no_content
else
render json: annotation.errors, status: :unprocessable_entity
end
end
def destroy
annotation = Annotation.find(params[:id])
annotation.destroy
head :no_content
end
private
def note_params
params.permit!
end
end
WORKS:
curl -i -X 'POST' http://localhost:3000/annotations -d 'quote=me&text=too'
DOES NOT WORK:
curl -i -X 'POST' -H "ContentType: application/json" http://localhost:3000/annotations -d '{"quote":"test3","text":"test4"}'
Started POST "/annotations" for 108.44.145.42 at 2013-12-12 17:28:58 -0500
Processing by AnnotationsController#create as */*
Parameters: {"quote"=>"hello", "text"=>"world"}
Completed 201 Created in 3ms (Views: 0.4ms | ActiveRecord: 0.0ms)
Started POST "/annotations" for 8.18.55.120 at 2013-12-12 17:31:24 -0500
Processing by AnnotationsController#create as */*
Parameters: {"{\"quote\":\"test3\",\"text\":\"test4\"}"=>nil}
Completed 201 Created in 3ms (Views: 0.4ms | ActiveRecord: 0.0ms)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment