Whipping Up a Rails API Real Quick
In this blog, I concisely outline the basic steps of creating a Rails RESTful API.
- Create New Rails Project
- Plan Models / Relationships
- Generate Resources
- Create / Migrate database
- Set Up Controllers
1. Create New Rails Project
First things first. Assuming Rails is installed, enter the following command into your terminal replacing
<my-project> with the current project name.
rails new <my-project> --api --database=postgresql
--apiflag will forgo including some of the extra junk a full Rails application has that we do not need. The
--databaseflag lets you specify the type of database you prefer. I chose PostgreSQL. If you do not include this flag, the default database will be SQLite.
For demonstration, I’ll create an Evil Dog Catcher API. I type out my command and hit enter.
2. Plan Models / Relationships
While Rails is installing, it’s a great time to plan the models and relationships. Having a clear idea of these ahead of time can save a lot of headaches.
For my Evil Dog Catcher API, I am going to have two models: Catchers and Dogs. The relationships will be:
- Catchers have many Dogs.
- Dogs belong to Catchers.
3. Generate Resources
Next, I’ll generate resources. This command quickly sets up migrations, models, controllers, and routes for each model name.
rails generate resource <model> <attribute>:<type> <attribute>:<type>
gis shorthand for
generate. Also, you can list another model’s name as an attribute with a type of
referencesto automatically create that model with a foreign key.
These will be my resource commands:
rails g resource catcher name:string city:string
rails g resource dog name:string breed:string catcher:references
References gives me a foreign key on the Dog model, but I have to manually go into the Catcher model set up a
4. Create / Migrate database
With all our files generated, it’s time to get the database going. First, I double check the migration files to make sure everything looks right.
Note line 6 of
null: false, I will not be able to create a dog unless it has the foreign key of a
Catcher. If you want to be able to create models without this, set
Since everything looks right, I will now create and migrate my database by entering these commands into the terminal:
Next, I test my progress in the following steps:
rails consoleopens the console.
- In the console, I create a
Catcher.create(name: 'test'), and a
Dog.create(name: ‘test’, catcher_id: 1)
- I test the relationship by entering
<ActiveRecord::Associations::CollectionProxy [#<Dog id: 1, name: “test”, breed: nil, catcher_id: 1, created_at: “2020–10–04 06:39:15”, updated_at: “2020–10–04 06:39:15”>]>
If there’s no errors, it’s time for the next step.
5. Set Up Controllers
For the final step, I will set up a basic controller and test it on the Rails server.
Then, I run
rails s or
rails server in the terminal. I open
http://localhost:3000/catchers and I see that everything is working.
For production purposes, you can open the CORS of this API to any website. First, install the CORS gem. Open the Gemfile, uncomment ‘rack-cors’, and run
Next, access the
config/applicationfile. Find the class
Applicationand — without altering any of the default code — insert the following:
config.middleware.insert_before 0, Rack::Cors do
:headers => :any,
:methods => [:get, :post, :delete, :put, :patch, :options, :head],
:max_age => 0
Remember to change the origins before deployment or this API will be available to anyone.
At this point the basic API has been created. The next steps will be to open up the CORS for whatever other sites will pull from it, and I set up the controllers to perform whatever actions you want to perform.
Hope this is helpful! Mostly I wanted to create a quick reference for setting up an API. If you have suggestions / corrections. Please comment or email me at