A Step by Step Guide: Creating an API Using Rails

Jahaira Marrero
5 min readMar 8, 2021

--

You’ve decided to sell everything you own and move across the country. Instead of creating a photo album and including the price under each item then posting it on Facebook for your friends to see, you opted to make an API for your website: www.immovingaccrossthecountrysoineedtoselleverything.com

STEP ONE

In your terminal, cd into the project folder and create a rails backend folder:

STEP TWO

In your terminal, generate your resource with the attribute (details) you want for each item:

STEP THREE

Since we are using PostgreSQL, you must first run create in your terminal (of course):

STEP FOUR

Create your tables…yes, we are still in the terminal:

STEP FIVE

Let’s navigate to the seed folder: db/seeds.rb. Type your data then seed your data.

Make sure that you actually create items from this array. I also like to include a string at the end so when I run db:seed, I’ll get a message otherwise there is no prompt unless there’s an error message.

You can now run rails db:seed in your terminal. If you see that awesome message, then you’re ready to rock and roll! If not, check your migration status and your seed data.

STEP SIX

Set up CORS (cross origin resource sharing). First, go to the gemfile and uncomment #gem ‘rack-cors’:

Next, run bundle install.

Next, navigate to the cors.rb file, config/initializers/cors.rb and uncomment the following:

Then change ‘example.com’ to ‘*’:

STEP SEVEN

Now it’s time to set up our routes. Of course, use what you need but I included the format for all seven actions. Don’t forget to comment out #resources, config/routes:

STEP EIGHT (with a twist)

Now it’s time to set up our controller. There a few differences to take note of:

  1. We are rendering all data to json.
  2. We no longer need to include.require when creating our private params.
  3. Path helpers are not needed.
  4. We are manipulating the index so that the items appear in alphabetical order. You sort your items by price, etc.

STEP NINE

Brace yourself, we’re now going to set up our serializers. The flow of the setup is similar to the steps up so I cut out the image guides and list them by number.

  1. Go to Gemfile and add: gem ‘active_model_serializers
  2. Run bundle
  3. Run spring stop (to reset rails g)
  4. Run rails g
  5. Run rails g serializer (Model Name). In this case, it would be ‘rails g Item’. Please note this should always be singular.
  6. Run rails s
  7. Update the attributes you would like to see in your localhost app/serializer/item_serializer.rb

8. Inside of config/initializers create a file ‘ams.rb’ or any file name you prefer and type the following:

This will allow you to call on other serializers (example: cut off associations).

STEP TEN

Let’s create some associations. For the moving sale, you are going to need a buyer and a purchase. The purchase will serve as the joiner.

  1. In the terminal, create the table for buyer.

2. Create your joiner table.

3. Update the macros in the model to reflect the associations you just established.

4. Update the routes.

5. Update the controller.

6. Update the serializer(s) to reflect the associations.

7. Add seed data.

8. Run rails db:reset

9. Run rails db:seed

--

--

Responses (1)