Initializing Rails API App

In this blog, we will set up a simple Rails API App from scratch. We won’t be going through building models or controllers here.

Installing Rails

Let’s install rails first, we will be using Rails 5.1 here for example, if you don’t have it run

And your rails version should be ready

Setting up a Rails API app

To install rails, run the command below it has got flags to skip the parts we won’t need in our project. Run it now we will pick it apart next:

Whew! That’s a mouthful. Let’s see about all those switches.

  • -T amounts to skipping the built-in testing framework. We’ll test our API with something other than MiniTest, so let’s drop it for now.
  • --skip-spring removes Spring from our application; that’s a personal preference. If you don’t know what Spring does and whether you should remove it, I urge you to find out and decide for yourself.
  • -C skips Action Cable. No web socket communication for us!
  • -M skips Action Mailer, which wouldn’t be of any use to us.
  • -d postgresql of course, sets us up for the PostgreSQL database.
  • --api - now we’re talking! This runs the Rails generator in the API mode, which sets us up for reading and writing JSON instead of pumping out HTML.

Now we can run tree on this new app, and… wow! When you compare that to a regular Rails app, this is really barebones. But we can trim this up a bit more:

  • An app/jobs directory was generated for us. We know that there will be no jobs in this application for the foreseeable future, so we can drop that.
  • There are four initializers that are commented out by default: backtrace_silencers.rb, application_controller_renderer.rb, inflections.rb and mime_types.rb. We may as well remove those.

One of the initializers will come in handy, though: cors.rb. We can uncomment what is already there and modify it to accept all origins:

This will make us happy campers in development and allows us to set it properly for production — when we will know where our front-end lives. As a side note, simply allowing * would also be fine if we were going to call our API from e.g. a mobile application.

To use this, we need to uncomment rack-cors in the Gemfile.

Setting up the database

We need to point our Rails app to the instance of PostgreSQL in different environments.

Our database.yml looks like this:

And our backend is all set, let’s get it running:

While working in production mode we need to add a flag RAILS_ENV=production after the command, we need this in commands like while running migrations or rollbacks and to start production server.


-p flag to specify the port

If everything went well, our database should be set up!!!

We have successfully set up a rails app from scratch to use as an API application with a PostgreSQL database.
Next, you can work on controllers, models, etc. as per your use cases.

Learning nd Developing.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store