DEV Community

reyes2981
reyes2981

Posted on

what i learned last week: how to build an api utilizing ruby on rails and connecting it to a javascript frontend pt 1

INTRO

Hi! In this series I'm going to go over how to build an API utilizing Ruby on Rails and connecting it to a JavaScript frontend. Remember, an application programming interface (API) is a connection between computers or between computer programs. It is a type of software interface, offering a service to other pieces of software.

Note, I'm learning that I code most efficiently when I work on features vertically and not horizontally. What do I mean by that? It means for the purpose of this series I'm going to through the process of creating a model, controller and view for a Player. Hmm...sound familiar?

MVC

API

First, to build out and specify properties of the API head over to the terminal. As you can see I'm adding commands that tell the system that the new project being created is an API and for this example has a postgresql database.

rails new example_api --api --database=postgresql

terminal command

Success! Once the command has been completed you should see something like the above image in your terminal.

Next, to work with the newly created API head over to your preferred text editor and open up the newly created directory.

files in text editor

Easy, right? Now that our API has been created we can start coding! As previously mentioned, I'll be using the MVC architecture to build out my program.

MODEL

The central component of the pattern. It is the application's dynamic data structure, independent of the user interface. It directly manages the data, logic and rules of the application.

Lets get to it and head over to the terminal to build out our Player model and run the following command:

rails generate model Player username:string email:string
player model
As you can see above, multiple things occurred when the above command was ran. Most importantly, a Player class and migration file were both simultaneously created.

Speaking of migration file...

migration file
Do you notice anything about the newly created migration file? Our Player already has a username and email attribute.

Sweet, I have my player model set up let me go create an instance of it!

new player error
WELP. An error?

I forgot to actually create the database! In the terminal run the following command:
rails db:create
database
Ok, now if I head over to the console lets see if I can create an instance of a Player.
error

Do you know what step I'm missing?
migration completed
If you guessed run rails db:migrate in the terminal you are correct! In its most basic form it just runs the up or change method for all the migrations that have not yet been run. If there are no such migrations, it exits. It will run these migrations in order based on the date of the migration. Note that running the db:migrate also invokes the db:schema:dump task, which will update your db/schema.rb file to match the structure of your database.

lets try creating a Player instance.
Success

Success!

To review, in the first part of this series I've built out an API, created a Player model, set up a database and finally confirmed that the created database is up and running! In the next entry I'll be going over the controller of my Player model. Thank you for reading!

Top comments (0)