Permalink
Browse files

10.1 Lecture includes json snippet and js example

  • Loading branch information...
1 parent 00a5b0b commit 68bd5f6ce997e9820fa4850ec4a632b8b8234106 @dimroc dimroc committed Jun 6, 2012
Showing with 35 additions and 3 deletions.
  1. +35 −3 lectures/10-restful-api/10.1-restful-rails.md
@@ -16,6 +16,19 @@ One Resource, Many Representations
* A TEXT representation: http://localhost:3000/things/1.txt
* A JSON representation: http://localhost:3000/things/1.json
+``` json
+{
+>>>>>>> 10.1 Lecture includes json snippet and js example
+ "thing": {
+ "created_at": "2012-06-06T11:45:38Z",
+ "description": "The beginning...",
+ "id": 1,
+ "name": "First Thing",
+ "updated_at": "2012-06-06T11:45:38Z"
+ }
+}
+```
+
The *Things* controller is already exposing the RESTful resource, but it needs to return different formats.
``` ruby
@@ -53,7 +66,7 @@ end
Use *curl* from the command line.
- $ curl -d "thing[name]=new%20thing&thing[description]=description" http://localhost:3000/things.json
+ $ curl -vd "thing[name]=new%20thing&thing[description]=description" http://localhost:3000/things.json
Responds with *201 Created*.
@@ -65,7 +78,7 @@ Responds with *201 Created*.
Updating Resources
------------------
- $ curl -X PUT -d "thing[name]=updated%20thing" -H "Accept: application/json" http://localhost:3000/things/1
+ $ curl -X PUT -vd "thing[name]=updated%20thing" -H "Accept: application/json" http://localhost:3000/things/1
Updating a record should return *200 OK*.
@@ -150,7 +163,26 @@ end
```
Using from JavaScript
----------------------
+=====================
+
+Creating a Thing:
+-----------------
+
+``` javascript
+createThing = function(name, description) {
+ $.post('/things.json', {
+ thing: {
+ name: name,
+ description: description
+ }
+ }).success(function(data) {
+ console.log("Created " + data.thing.name + " with id " + data.thing.id);
+ });
+};
+```
+
+Search and Autocomplete:
+-----------------------------
We can use [jquery-ui-bootstrap-rails](https://github.com/jaimie-van-santen/jquery-ui-bootstrap-rails) gem to style JQuery UI.

0 comments on commit 68bd5f6

Please sign in to comment.