Ruby on Rails pt 2
Hello and welcome back to the second part of our creating an API using Ruby on Rails tutorial. If you haven’t been following along, I have another blog with the beginning steps to get you set up and then you can pick back up with this tutorial as well. Anddd here we go.
So, the last step that we did in the previous tutorial was create our first table. But, I had actually forgot a step but it is no need to worry. This is how easy it is to forget. In our terminals, lets run a: “rails db:create”. This is going to create our database for us and by default, we’re going to be using an SQLite database, which you can find in your “db” folder. Great, now some fun stuff is about to begin. After we create our table, you can now see a few things added inside your backends folders. If you look now, you should notice inside your app folder, two folders named “controller” and “model”. If you check those our, you’ll see a new controller “user_controller” and a new model “user.rb” has been created for you for free. I love Ruby on Rails! Also check our your db folder and see a new migration in there.
I didn’t do that, did you? Of course not, Rails got our back though. Awesome, so now that we have our table and everything is looking good. Now, and it is very important to remember, we need to migrate our table. Rails is cool and has easy to remember commands. Need to migrate? How about just a simple:
Boom! Easy-peasy, or however you spell it. Now we have our schema, our schema is going to let us know that our migration has been successful. And you can check that our and it just shows a different laid out version of our table but is still our table. Cool. Next thing we need our some seeds. Seeds are used to create instances. So to give you a better understanding of what a seed does, it basically lets you create a user using the columns you typed in before. That being name and age for this example. So to create some seeds, we need to go into our seeds file or “seeds.rb”. Inside this file, we can but don’t have to, assign a variable to our seeds. But we take our seed and use a capitol “U” for users to let Ruby know this is a class. The code will look something like this:
Either way is fine and will both work. Next, we want to seed these. Can you guess how? Using Ruby on Rails easy syntax :
We are on our way! Just a few more steps and we are going to have a fully functioning API! Alright cool, now what we want to do is run a “rails c” in our terminal. What this is, is Ruby’s console, and we use this to check and see that our seeds were in fact, created. So after running “rails c”, lets type in the terminal “User.all” and we should see our two users that have been created. That means it works! Awesome, onto the next step. Depending on what you want to do with your tables, you may need to go into your models. Models is where you create your relationships, if applicable. But for this tutorial, we don’t have any so we can go right to our user’s controller, located in our app file, under controllers. We are almost there!
Here we need to create an index method. What this method will do, is render our users to our server and show them as JSON. To make the method is easy, and if you can remember this, you will always be gucci when trying to see your code. So we make our method, and inside the method we need to use an instance variable “@user” so we can access it and use it again throughout our controller. So for “@user” we want to equal “User.all”. Reason why is because we want to see all of our users. Then we will render our json with our instance variable. The code should look something like:
Awesome. Now are you ready for this bad boy? Inside your terminal, run a command: “rails s”. This is going to run your rails server, default rails server will take you to a web address of “http://localhost:3000”. After you get to this point, let’s add a forward slash and then the word “users” like so:
And bang! Holla at ya boy! You have just created your first API! Follow for later videos. I will explain a little bit more in depth, but this is how we do it! Thank you guys for following. Catch you all next time!