Closed nriser closed 7 years ago
I think I may have solved it, but would appreciate it if anyone could confirm this.
I first ran a POST request to make a new game using a sample player x token (player id = 16). This resulted in a Completed 201 response.
curl --include --request POST http://localhost:4741/games \
--header "Authorization: Token token=BAhJIiVjYTUwNDkwMGQ5NWRlMGNlMWVjOWVjZjdhZmIzYmEyMwY6BkVG--35d8e6fc70686b4b9cae8f3aa3dd530285a59455" \
--data ''
echo
response from rails:
Started POST "/games" for ::1 at 2017-04-11 21:29:26 -0400
Processing by GamesController#create as */*
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."token" = $1 LIMIT 1 [["token", "ca504900d95de0ce1ec9ecf7afb3ba23"]]
(0.2ms) BEGIN
SQL (14.2ms) INSERT INTO "games" ("player_x_id", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["player_x_id", 16], ["created_at", "2017-04-12 01:29:27.052711"], ["updated_at", "2017-04-12 01:29:27.052711"]]
(12.4ms) COMMIT
[active_model_serializers] Rendered GameSerializer with ActiveModelSerializers::Adapter::Json (33.54ms)
Completed 201 Created in 194ms (Views: 59.7ms | ActiveRecord: 27.4ms)
it seems now that a new game with id=1 has been created with player_x.
(Question: It took me a while to guess that the game id is equal to 1 (because it's the first game I made), but how would I have known that just by reading the rails response?)
Then I ran a GET request to get info on game id=1.
curl --include --request GET http://localhost:4741/games/1 \
--header "Authorization: Token token=BAhJIiVjYTUwNDkwMGQ5NWRlMGNlMWVjOWVjZjdhZmIzYmEyMwY6BkVG--35d8e6fc70686b4b9cae8f3aa3dd530285a59455" \
--data ''
echo
response from rails:
Started GET "/games/1" for ::1 at 2017-04-11 21:41:31 -0400
Processing by GamesController#show as */*
Parameters: {"id"=>"1"}
User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."token" = $1 LIMIT 1 [["token", "ca504900d95de0ce1ec9ecf7afb3ba23"]]
Game Load (1.7ms) SELECT "games".* FROM "games" WHERE (player_x_id = 16 or player_o_id = 16) AND "games"."id" = $1 LIMIT 1 [["id", 1]]
[active_model_serializers] User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 16]]
[active_model_serializers] Rendered GameSerializer with ActiveModelSerializers::Adapter::Json (63.27ms)
Completed 200 OK in 109ms (Views: 67.0ms | ActiveRecord: 2.8ms)
When creating a new game we don't assign it an ID, that gets handled on the back end. We just want to POST to the /games URL.
@jordanallain Thank you for your reply! I managed to figure that out, but what part of the rails response shows the newly created game id? Is it where it says AND "games"."id" = $1
?
have you logged the response to the console in your success function that runs after creating a game?
Yes, I posted in my second comment. Here is the success response from rails that I get after the POST request:
Started POST # "/games" for ::1 at 2017-04-11 21:29:26 -0400
Processing by GamesController#create as */*
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."token" = $1 LIMIT 1 [["token", "ca504900d95de0ce1ec9ecf7afb3ba23"]]
(0.2ms) BEGIN
SQL (14.2ms) INSERT INTO "games" ("player_x_id", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["player_x_id", 16], ["created_at", "2017-04-12 01:29:27.052711"], ["updated_at", "2017-04-12 01:29:27.052711"]]
(12.4ms) COMMIT
[active_model_serializers] Rendered GameSerializer with ActiveModelSerializers::Adapter::Json (33.54ms)
Completed 201 Created in 194ms (Views: 59.7ms | ActiveRecord: 27.4ms)
it seems now that a new game with id=1 has been created with player_x, but I'm wondering if ""/games" for ::1 " in the beginning is where we can confirm that it created a new game with id=1? I knew that it would be game id=1 because it was the first game I created, but how would I know in the future what new game id has just been created just by reading the rails response?
no i'm saying in your javascript, in the success function that receives the response from your ajax call, you can console.log(response) or data or whatever you call it to see what is returned from the POST request.
I see what you're saying. This is a curl request though so I haven't done the ajax call yet. Just from testing with the curl script alone, I wanted to make sure that I know what game id was created when I made the POST request. I'm sorry for the lack of clarity.
The response below is the rails response after I did a curl request, not ajax call:
Started POST # "/games" for ::1 at 2017-04-11 21:29:26 -0400
Processing by GamesController#create as */*
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."token" = $1 LIMIT 1 [["token", "ca504900d95de0ce1ec9ecf7afb3ba23"]]
(0.2ms) BEGIN
SQL (14.2ms) INSERT INTO "games" ("player_x_id", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["player_x_id", 16], ["created_at", "2017-04-12 01:29:27.052711"], ["updated_at", "2017-04-12 01:29:27.052711"]]
(12.4ms) COMMIT
[active_model_serializers] Rendered GameSerializer with ActiveModelSerializers::Adapter::Json (33.54ms)
Completed 201 Created in 194ms (Views: 59.7ms | ActiveRecord: 27.4ms)
Review https://github.com/ga-wdi-boston/game-project-api#create
The create action expects a POST with an empty body (e.g '' or '{}' if JSON). If the request is successful, the response will have an HTTP Status of 201 Created, and the body will contain JSON of the created game with player_x set to the user calling create.
And conceptually, why would a game have an ID if it hasn't been created yet? The API says the response will be the game object that contains its ID that was created by the database.
@MicFin Thank you for your response. I understand that ...
{
"game": {
"id": 3,
"cells": ["","","","","","","","",""],
"over": false,
"player_x": {
"id": 1,
"email": "and@and.com"
},
"player_o": null
}
}
... is the response that we get after we do an ajax call, and that the game ID won't be generated until you create it. I'm not sure if I'm getting my question through properly, but I'm wondering actually only about the curl request and the response I get from rails.
As mentioned in my second comment, I ran a post request from curl script and got a Completed 201 response. My understanding is that at that point, a new game with id=1 has been created with player_x.
Started POST "/games" for ::1 at 2017-04-11 21:29:26 -0400
Processing by GamesController#create as */*
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."token" = $1 LIMIT 1 [["token", "ca504900d95de0ce1ec9ecf7afb3ba23"]]
(0.2ms) BEGIN
SQL (14.2ms) INSERT INTO "games" ("player_x_id", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["player_x_id", 16], ["created_at", "2017-04-12 01:29:27.052711"], ["updated_at", "2017-04-12 01:29:27.052711"]]
(12.4ms) COMMIT
[active_model_serializers] Rendered GameSerializer with ActiveModelSerializers::Adapter::Json (33.54ms)
Completed 201 Created in 194ms (Views: 59.7ms | ActiveRecord: 27.4ms)
I was able to confirm it in my curl GET request response,
Started GET "/games/1" for ::1 at 2017-04-11 21:41:31 -0400
Processing by GamesController#show as */*
Parameters: {"id"=>"1"}
User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."token" = $1 LIMIT 1 [["token", "ca504900d95de0ce1ec9ecf7afb3ba23"]]
Game Load (1.7ms) SELECT "games".* FROM "games" WHERE (player_x_id = 16 or player_o_id = 16) AND "games"."id" = $1 LIMIT 1 [["id", 1]]
[active_model_serializers] User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 16]]
[active_model_serializers] Rendered GameSerializer with ActiveModelSerializers::Adapter::Json (63.27ms)
Completed 200 OK in 109ms (Views: 67.0ms | ActiveRecord: 2.8ms)
My question is, where in the curl script rails response after the POST request can I see that the game id=1 was created?
AND "games"."id" = $1 LIMIT 1 [["id", 1]]
<--
@jordanallain Great, that makes sense. thank you!
@MicFin Thank you for chatting with me.
My issue was that I was checking the rails response only, and not the response I was getting in my working terminal. I see that the game ID is explicitly written in the response! I apologize for the confusion.
$ TOKEN="BAhJIiVlY2ZkYzIxOTRkNzQ1NzlmNTNlYTA2YmI4MDY1YzZhNwY6BkVG--6538aee6891e5b6873ea220139ecb7aeebc2ec13" sh scripts/game-create.sh
HTTP/1.1 201 Created
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
Cache-Control: no-cache
Content-Type: application/json; charset=utf-8
X-Request-Id: 9ab43b59-3b39-4395-bcba-07f1fba7768d
X-Runtime: 0.062126
Vary: Origin
Transfer-Encoding: chunked
{"game":{"id":6,"cells":["","","","","","","","",""],"over":false,"player_x":{"id":13,"email":"ee"},"player_o":null}}
I'm trying to make my curl request for the create game request, but I keep getting a 404 Not Found response. I've tried two ways, one with POST (as it's written in the game api project repo), and another with GET. The token and ID were retrieved after I signed in. I apologize in advance if I'm missing any other info! I'd appreciate any feedback.
curl request in project-tictactoe/scripts/game-create.sh:
error message on grunt server:
command I run in terminal, along with the error message below:
I've also tried it with a GET request instead of a POST:
curl request in project-tictactoe/scripts/game-create.sh:
error message on grunt server:
command I run in terminal, along with the error message below: