Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJustin Black
24,793 Pointscant get curl command to work
Running the command:
curl -i -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"title":"The Title Goes Here"}' http://localhost:3000/api/todo_lists
results in the following response:
HTTP/1.1 500 Internal Server Error
X-Frame-Options: SAMEORIGIN
X-Xss-Protection: 1; mode=block
X-Content-Type-Options: nosniff
Content-Type: application/json
Cache-Control: no-cache
X-Request-Id: 4886e13a-d7db-437f-a40c-3db78e038e81
X-Runtime: 0.018863
Server: WEBrick/1.3.1 (Ruby/2.2.1/2015-02-26)
Date: Sun, 19 Apr 2015 07:48:16 GMT
Content-Length: 0
Connection: Keep-Alive
Set-Cookie: request_method=POST; path=/
I've researched and tried a few different things:
skip_before_filter :verify_authenticity_token
skip_before_action :verify_authenticity_token
and
protect_from_forgery :except => [:create]
all in the api TodoListsController and the ApplicationBase. I even tried making an api specific application base that shuts protect from forgery off completely.
Code for the full TodoListsController is:
class Api::TodoListsController < ApplicationController
skip_before_filter :verify_authenticity_token
def index
render json: TodoList.all
end
def show
list = TodoList.find(params[:id])
render json: list
end
def create
list = TodoList.new(list_params)
if list.save
head 200
else
head 500
end
end
private
def list_params
params.require("todo_list").permit("title")
end
end
my IDE (RubyMine) shows me this error when hovering over ':verify_authenticity_token'
"Expected method name from current or parent classes up to 'ActionController::Base', but found ':verify_authenticity_token'. or hash expected"
Which I guess is fine, the server output shows me this:
Started POST "/api/todo_lists" for 127.0.0.1 at 2015-04-19 00:57:26 -0700
Processing by Api::TodoListsController#create as JSON
Parameters: {"title"=>"The title goes here!", "todo_list"=>{"title"=>"The title goes here!"}}
(0.0ms) begin transaction
(0.0ms) rollback transaction
Completed 500 Internal Server Error in 3ms (ActiveRecord: 0.5ms)
[UPDATE] I edited my create method to be like this:
def create
todo_list = TodoList.new(list_params)
if todo_list.save
render json: :show
else
render json: todo_list.errors
end
end
and it reveals, being that we're working with odot, that description is required and can't be nil. So adding that in the list_params private method, and submitting it with my post gave me what I was expecting.
1 Answer
Justin Black
24,793 PointsI edited my create method to be like this:
def create
todo_list = TodoList.new(list_params)
if todo_list.save
render json: :show
else
render json: todo_list.errors
end
end
and it reveals, being that we're working with odot, that description is required and can't be nil. So adding that in the list_params private method, and submitting it with my post gave me what I was expecting.