Skip to content

CRUD Curl Jets Tutorial ActiveRecord

Tung Nguyen edited this page Dec 24, 2017 · 3 revisions

CRUD Curl Jets Tutorial

Here's a CRUD demo for Jets with curl. Note, I'm using jq to pretty print the json responses.

Commands Summarized

New Jets Project

jets new store
cd store
jets server # do this in another terminal
open http://localhost:8888 # should see Jets welcome page

Scaffold with ActiveRecord

jets generate scaffold Toy name --api
jets db:create db:migrate

toys#index

$ curl -s http://localhost:8888/toys | jq .
[]

toys#create

$ curl -s -X POST http://localhost:8888/toys -d '{
  "toy": {
    "name": "train"
  }
}' | jq '.'
{
  "id": 1,
  "name": "train",
  "created_at": "2017-12-24T02:47:20.164Z",
  "updated_at": "2017-12-24T02:47:20.164Z"
}

toys#show

$ curl -s http://localhost:8888/toys/1 | jq '.'
{
  "id": 1,
  "name": "train",
  "created_at": "2017-12-24T02:47:20.164Z",
  "updated_at": "2017-12-24T02:47:20.164Z"
}

toys#update

$ curl -s -X PUT
  http://localhost:8888/toys/1
  -d '{
  "toy": {
    "name": "train set"
  }
}' | jq .
{
  "id": 1,
  "name": "train set",
  "created_at": "2017-12-24T02:47:20.164Z",
  "updated_at": "2017-12-24T02:49:00.860Z"
}

toys#delete

$ curl -s -X DELETE http://localhost:8888/toys/1 | jq .
{
  "deleted": true
}