You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: rails6/en/chapter03-presenting-users.adoc
+17-17Lines changed: 17 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -51,7 +51,7 @@ invoke active_record
51
51
create test/fixtures/users.yml
52
52
----
53
53
54
-
NOTE: In the MVC design patern, the model is the element that contains the data as well as the logic related to the data: validation, reading and recording. So first we will create this part.
54
+
NOTE: In the MVC design patern, the model is the element containing the data as well as the logic related to the data: validation, reading and recording. So first we will create this part.
55
55
56
56
This command generates a lot of files! Don't worry, we'll review them one by one.
57
57
@@ -74,7 +74,7 @@ end
74
74
75
75
NOTE: inserted date at the beginning of the migration file name should be different for you since it corresponds to the migration creation date.
76
76
77
-
We will modify a small this migration in order to add some database validations. With Rails it is common practice to make these verifications directly in the Ruby model but it is good practice to do so also in the database schema.
77
+
We will modify a little bit this migration in order to add some database validations. With Rails it is common practice to make these verifications directly in the Ruby model but it is good practice to do so also in the database schema.
78
78
79
79
We will therefore add two additional constraints:
80
80
@@ -111,7 +111,7 @@ NOTE: This command will convert our migration into a SQL query that will update
111
111
112
112
==== Model
113
113
114
-
We need to first add the `devise` gem into the `Gemfile`
114
+
First we need to add the `devise` gem into the `Gemfile`
Then run the `bundle install` command to install it. Once the bundle command finishes, we need to run the devise install generator:
167
+
Then run the `bundle install` command to install it. Once the bundle command finishes, we need runing the devise install generator:
168
168
169
169
[source,bash]
170
170
----
@@ -262,7 +262,7 @@ RSpec.describe User, type: :model do
262
262
end
263
263
----
264
264
265
-
Because we previously prepare the test database, with `rake db:test:prepare`, we just simply run the tests:
265
+
Because we previously prepare the test database, with `rake db:test:prepare`, we just simply run tests:
266
266
267
267
[source,bash]
268
268
----
@@ -299,20 +299,20 @@ This command will create a `users_controller_spec.rb`. Before we get into that,
299
299
300
300
.Most common http codes
301
301
****
302
-
The first digit of the status code specifies one of five classes of response; the bare minimum for an HTTP client is that it recognize these five classes. A common list of used http codes is presented below:
302
+
The first digit of the status code specifies one of five classes of responss. The bare minimum for an HTTP client is that it recognize these five classes. A common list of used http codes is presented below:
303
303
304
304
* `200`: Standard response for successful HTTP requests (It is commonly on GET requests)
305
305
* `201`: The request has been fulfilled and resulted in a new resource being created (After POST requests)
306
306
* `204`: The server successfully processed the request, but is not returning any content (It is usually a successful DELETE request)
307
307
* `400`: The request cannot be fulfilled due to bad syntax.
308
-
* `401`: Similar to 403 Forbidden, but specifically for use when authentication is required and has failed or has not yet been provided
308
+
* `401`: Similar to 403 Forbidden, but specifically used when authentication is required and has failed or has not yet been provided
309
309
* `404`: The requested resource could not be found but may be available again in the future (Usually GET requests)
310
310
* `500`: A generic error message, given when an unexpected condition was encountered and no more specific message is suitable.
311
311
312
312
For a full list of HTTP method check out the article on http://en.wikipedia.org/wiki/List_of_HTTP_status_codes[Wikipedia] talking about it
313
313
****
314
314
315
-
To keep our code nicely organized, we will create some directories under the controller specs directory in order to be consistent with our current setup. There is also another set up out there which uses instead of the `controllers` directory a `request` or `integration` directory, I this case I like to be consistent with the `app/controllers` directory.
315
+
To keep our code nicely organized, we will create some directories under the controller specs directory in order to be consistent with our current setup. There is also another set up out there which uses instead of the `controllers` directory a `request` or `integration` directory, In this case I like being consistent with the `app/controllers` directory.
316
316
317
317
[source,bash]
318
318
----
@@ -355,7 +355,7 @@ RSpec.describe Api::V1::UsersController, type: :controller do
355
355
end
356
356
----
357
357
358
-
So far, the tests look good, we just need to add the implementation. It is extremely simple:
358
+
So far, tests look good, we just need to add the implementation. It is extremely simple:
359
359
360
360
[source,ruby]
361
361
.app/controllers/api/v1/users_controller.rb
@@ -444,7 +444,7 @@ $ git commit -m "Adds show action the users controller"
444
444
445
445
=== Testing endpoints with CURL
446
446
447
-
So we finally have an endpoint to test. There are plenty of options to start playing with. The first that come to my mind is using http://curl.haxx.se/[cURL] because is built-in on almost any Linux distribution and of course on your Mac OSX. So let’s try it out:
447
+
So we finally have an endpoint to test. There are plenty of options to start playing with. The first coming to my mind is using http://curl.haxx.se/[cURL] because is built-in on almost any Linux distribution and of course on your Mac OSX. So let’s try it out:
448
448
449
449
NOTE: Remember our base URI is `api.market_place_api.dev`.
450
450
@@ -453,7 +453,7 @@ NOTE: Remember our base URI is `api.market_place_api.dev`.
This will throw us an error. Well you might expect that already because we don’t have a user with `id` equals to 1. Let’s create it first through the terminal:
456
+
This will throw us an error. Well you might expect that because we don’t have an user with `id` equals to 1. Let’s create it first through the terminal:
457
457
458
458
[source,bash]
459
459
----
@@ -493,9 +493,9 @@ $ git commit -m "Updates application controller to prevent CSRF exception from b
493
493
494
494
=== Creating users
495
495
496
-
Now that we have a better understanding on how to build endpoints and how they work, it’s time to add more abilities to the API. One of the most important is letting the users actually create a profile on our application. As usual we will write tests before implementing our code extending our testing suite.
496
+
Now that we have a better understanding on how to build endpoints and how they work, it’s time to add more abilities to the API. One of the most important is letting the users create a profile on our application. As usual we will write tests before implementing our code extending our testing suite.
497
497
498
-
Creating records in Rails as you may know is really easy, the trick when building an api is which is the best fit for the HTTP codes to send on the response, as well as the actual `json response`. If you don’t totally get this it will probably be more easy on the code:
498
+
Creating records in Rails as you may know is really easy, the trick when building an api is the best fit for the HTTP codes to send on the response, as well as the actual `json response`. If you don’t totally get this it will probably be more easy on the code:
499
499
500
500
*Make sure your repository is clean and that you don’t have any commits left, if so place them so we can start fresh.*
501
501
@@ -548,7 +548,7 @@ end
548
548
549
549
There is a lot of code up there but don’t worry I’ll walk you through it:
550
550
551
-
* We need to validate to states on which the record can be, valid or invalid. In this case we are using the `context` clause to achieve this scenarios.
551
+
* We need to validate states on which the record can be, valid or invalid. In this case we are using the `context` clause to achieve this scenarios.
552
552
* In case everything goes smooth, we should return a `201` HTTP code which means a record just got `created`, as well as the JSON representation of that object.
553
553
* In case of any errors, we have to return a `422` HTTP code which stands for `Unprocessable Entity` meaning the server could save the record. We also return a JSON representation of why the resource could not be saved.
554
554
@@ -620,7 +620,7 @@ $ git commit -m "Adds the user create endpoint"
620
620
The pattern for *updating* users is very similar as *creating* new ones. If you are an experienced Rails developer you may already know the differences between these two actions:
621
621
622
622
* The `update` action responds to a PUT/PATCH request.
623
-
* Only the `current_user` should be able to update their information, meaning we have to enforce a user to be authenticated. We will cover that on next chapters
623
+
* Only the `current_user` should be able to update their information, meaning we have to enforce an user to be authenticated. We will cover that on next chapters
624
624
625
625
As usual we start by writing our tests:
626
626
@@ -673,9 +673,9 @@ RSpec.describe Api::V1::UsersController, type: :controller do
673
673
end
674
674
----
675
675
676
-
Getting the tests to pass requires us to build the `update` action on the `users_controller.rb` file as well as adding it to the `routes.rb`. As you can see we have to much code duplicated, we’ll refactor our tests in next chapter.
676
+
Getting the tests pass requires us to build the `update` action on the `users_controller.rb` file as well as adding it to the `routes.rb`. As you can see we have too much code duplicated, we’ll refactor our tests in next chapter.
0 commit comments