Lessons: 14Length: 2.1 hours

Next lesson playing in 5 seconds

  • Overview
  • Transcript

2.1 Tables

Let’s begin by implementing a basic mechanism for managing tables in the restaurant. We’ll use the scaffold generator to accomplish it: notice the differences between the rails-api generator and the regular rails generator.

Related Links

2.1 Tables

After bootstrapping a new Rails-API project, we can start delving into the actual code. Remember how we had a table model that we needed to handle. Well, we're gonna take care of that in this very lesson. The process of spawning a new resource is basically the same as with any other Rails application. Remember that instead of typing Rails just like that we need to pass and dash api and then generate. We'll pass in a scaffold and then the name of the model. I'm gonna pass in table like so. Table is the name of our model. And now what kind of attributes should this table model have? Well, for one thing, I recall we mentioned a number which would be an integer, like that, and then, for example, a number of seats, which would also be an integer. So we'll do just that. This process is really as straight forward as any other Rails app. Just press Enter and we'll have a bunch of different things already taken care of for us. Now the good part is when we go to the editor and check app controllers and then tables controller, you will see the same amount of code that you would have in a normal rails app. But in this specific scenario, since we are dealing with an API version, we will get a specific kind of rendering. Instead of handling all of the things regarding views and templates and ERB files, and stuff like that, we will just be rendering json code. So, the explicit declaration as you see here in line 24, when actually creating a new table, it will return a json structure with the table right there, and then with a status code and a location. The status code relates to the http response. It'll return 201. The location is also another kind of http variable and the json data is returned as a result of interpreting the table model. Remember that if the process is not as stream lined as we wish it would, for example there is some validation rule that we didn't fulfill. The errors will be rendered instead. So we will just render the errors as you see here with the 422 status code, which is the one that's represented by unprocessable entity. So I've mentioned the create action, but there are other methods as well. As you can see, we have before filters the same way we would have in regular Rails, along with the standard methods. Now there's one thing that you should know. If you go to config/routes, you will see resources table but we will have an exception of loading new and edit. These will not be part of resources. Because we don't have any way of returning an html template with for example a form to fill that data for the table. That doesn't exist. We resort explicitly to the post method here, with create, and also the one in update, here in line 32. So, regardless of what you want to do, we won't have any HTML. And because of that, we won't need these new and edit routes. As a reflection, you won't see this data in the controller. So those specific methods, new and edit, are not here. We only have what we need and nothing less. Now, let's go ahead and try to, for example, test this out. How do we actually create tables, update them, delete them, how do we do that? Well, for that specific purpose, I'm going to open either a new tab or just run a command in the console, or just record a script .ini file, and run that script. Now if you don't have Curl installed you can check this link that I posted in the description below in order to install it in your machine. So let's go to the projects folder, there you go and now I'm just gonna type in curl http://localhost:3000 it's the usual really, and then /tables. If you don't know Curl basically emulates a request from a browser but it just returns whatever comes from the server as raw as it can be. So if I press Enter now you will see that this connection is refused because we don't have any server. I'm just gonna pour really quick, just gonna open it up and in one hand, I'll have the server running. Just type Rail server or even Rails API server. It's basically the same thing, because at the end of it all, it's just a rack application. And it will run regardless of being API or not. And with that out of the way, I'm going to go and create a new pane and type in that command. So, curl and then local host port 3000 and then slash tables. Most likely we will have an error. There you go. As you can see HTML is indeed rendered to us even though it shouldn't, but regardless of that we can actually check the other side. This one contains a basic log and it says that migrations are pending. So we're gonna do just that. Let's type in rake db migrate, right away. Remember that if you want to configure your own version of the database, maybe use a different driver or something like that, go ahead. For now I'm just going to stick with the defaults of using SQL Lite. So now that the migrations are done, we can run that very same script again. So curl http localhost, 3000 tables. Now the request is right here. Can you see that? So we're going to select everything from the tables table. It didn't take very long for now because we have an empty list. And notice how that's all we have. Let's try and pass in eight post requests. And how do we do that? Well, I'm gonna type in a really quick Curl script just like so. I'm gonna make sure that the post method is issued and then I wanna pass in some data. You use -D with some content inside. Now what I'm gonna do here is, for example, create a table number. There you go. Maybe we're just going to start with one. And then table seats will be, for example, two people. So a nice romantic place. Now, we're gonna go to the same local host 3000 slash tables. This is very important that you fix, because we are introducing a post request to slash tables, this is going to go to the create action, the same way as we would with a standard form, with some data to fill in that table. So, pressing Enter now, you will see something different. You will see a hash with the information on that table. Oddly enough, you will see that we have a null here. Most likely we have done some sort of a mistake when trying to fill in this data. So instead of this, I'm gonna actually run a new script. I'm gonna go to our previous window with the editor, and I'm going to create a Curl file, which will actually be an executable file. So I'll use chmod plus x, and then bin/curl. With this, we can run this file the same way as we would with any other script. So what we want to do here is just type in whatever code we want to run. In this case, I'll type in curl -x post and the same data as we had previously. Now instead of using -d, I'm gonna use something slightly different. I wanna pass in a json file. Okay, this is very good when we want to do some testing. In this case, since we want to try this over and over again, it's a good idea to fix this kind of code in a script file and then just run it over and over again. So I'm gonna paste the snippet of code that will do exactly what we want to achieve. Let me explain to you what this strong variant of using Curl really is about. We have -v that will basically act as a verbose flag. It will give us more information on the request and the response as well. Then -H allows us to send in headers, custom headers that is, into the request. In this specific case we want content type to be application/json. What this means is when we shove in some data in to the request, we know for a fact that it's going to be json data. So we use that, and we read the contents from a file. That's why we use @ here. So -d@bin/table.json will basically transform the data that's going to be in the file as data in json into the request. Finally we just have the regular url. We've had it before and we're gonna send it in again. The back slashes are just to allow for multi-line commands. Ideally we would have this in one single line, but for the sake of readability, we add in backslash to escape the new line. With this we can just create our table.json file. There you go. And in here, we're gonna have a table variable, which will have for example, a number. Let's now say that this is number 2. And then the number of seats is going to be something like 4. This will be a Goodall family seat, so let's just make sure that this is indented properly and all the syntax is respective. So we'll need to add in quotes in each different key. There you go. It seems that now it looks like a valid json file. Now all we have to do is type in bin/curl in the command line. Basically we will run this script as if it were any other one. Let's do that, I'll type in bin curl and there you go. So this is the entire content of the request of the Curl command, because we have the verbose flag we will see what's going in, what's coming out and the actual response, this whole line, and notice how the whole line has an ID, a number, a number of seats, and the time stamps. Also, notice the content type right here, the host, the method that's being printed out, so it's a POST command, and then all the options and headers that come from the server as part of the response, e-tags included. So now that we have two different tables, we can type in something like curl http://localhost:3000/tables and we will get the two orders. There you go. We have successfully implemented a basic scaffold for listing, showing, creating, editing, and deleting table objects. So, from here, it is going to be a lot easier with the remaining data entities. I didn't try everything around the generated content because I really don't feel like it's useful or provides any value more than you already know. In this specific scenario we tried listing all of the tables and creating a new one resulting to curl. The same thing goes for updating and deleting. Instead of using post, you use either patch or delete, respectively, depending on whether you want to update or delete a certain record.

Back to the top