How to create an API in RoR pt 1.

Welcome to the create an API tutorial on Ruby on Rails. Today we’re going to be setting up a few things to ensure we get our RoR backend up and running correctly. If you follow this guide, you should always be good to go. There will be a part II to clear some things up and add a little more but for now.. let’s hop into it.

First when creating your Ruby on Rails backend, you want to make sure that you set it up in your project exactly where you want. You can call it whatever you want but for me, I just stick to plain old “backend.” Just makes it easier for myself when going through this process. So first thing we need to do is “cd” which means change directory into the folder we want to create this backend. Best practice is doing so from your terminal, but you can also use your code editor as well.. I’m going to use my code editor. The first command we need to run is: “rails new “whatever you want to call this” — api” Like so..

After we run this command, you should see all the cool stuff that Rails gives us. If you open your backend folder, you will notice a bunch of already given folders. This is awesome! Saves a bunch of time and work! Cool, so next thing we want to do is make sure all of our packages or “bundles” are installed. When using rails, you often times are going to want to enable your “cors.” Reason why is because Cross-origin resource sharing (CORS) is a mechanism that allows restricted resources on a web page to be requested from another domain outside the domain from which the first resource was served. A web page may freely embed cross-origin images, stylesheets, scripts, iframes, and videos. To find this, we want to open our backend folder and find the file named “Gemfile.” In Gemfile, you will also see a bunch of pre-given packages Ruby was nice enough to give us. So let’s find our cors and enable it, by commenting it back in. If you’re using VSCode, it’ll be on line 26. This is only half the battle. Next, and this is key because you’ll run into some issues if you don’t do this.. You need to now go into your “config” folder and look for another folder named “initializers”. From there, you should see a file named cors.rb.

Inside this cors.rb file, you will need to comment this back in as well. And change the origins “example.com” to simply an asterisks (*). This allows our resource to be accessed by any origin. Awesome. After that, let’s go back into our terminal and run a “bundle install”. You can run this straight from the terminal and then watch it go! This is updating all your gems and is going to make your life a lot easier throughout adding your code for your project! We are on our way!

So, in other lesson, I may go through the long way of creating this api. What that means is, in Ruby on Rails, there is a command you can run that will give your models, views, controllers and a few more things. Or you can run each one of these specifically. It’s not hard or time consuming, but we want to be quick! Some times you won’t need the views or something else, which would be a time to do it individual. But for now, we’re going to use the shortcut. So with this shortcut, we’re going to be creating our tables. Basically, our template of what we’re going to use. So let’s create a single table called “users”. Our user is going to have a name and an age, to keep it simple.

Also, another thing Ruby gives us is whenever you are creating a table and want to use a string, it is set to string by default. So if you do not specify, your attribute is going to be a string. Of course, you can change this later also, but why do the extra step? Each attribute will need some kind of datatype, so for this example, “age” is definitely a number for anything so it needs to be defined as a number. Alright, well this has been part 1 of creating an API in Ruby on Rails, stick around for part 2 so we can see some magic happen.

Hi, my name is Rodney Smith II.