How To Develop REST API with Rails 5.0 API Only


Rails is very complete framework and this means “too much” environment if we just want to build rest api on top of Rails.

That’s why there is Rails API which is subset of a normal Rails application that provide more lightweight version of rails and it is also a bit faster than a normal Rails.

Rails 5 integrates Rails API at the core, so here are the steps to build REST API using Rails API only that comes with Rails 5.0.

On this case I will build example app for 2 domain model: Video and Image with Many to One relation, and this post I only explain steps to build REST API for videos only without relation to Image.

First time, before we start please make Sure you already installed Rails 5 and currently using Rails 5 by run this command:

rails -v

Note: If you haven’t installed and setup the Rails 5.0 you can read my previous post here “Install Rails 5.0 for Ruby Dev“.

Generate Rails API project code:

We can generate Rails API project code by running:

rails new your_project_name --api

The –api tells rails we will use rails api only for our application.

After that we need to go inside the ‘your_project_name’ folder to do the next steps.

We can see there are some folders generated inside it as common Rails API project application structure

Generate Video Domain Model

For Video sample REST API I use generate scaffold from Rails to generate the scaffolding code:

> bin/rails g scaffold video title description language share_url download_url duration:integer file_size:integer share_url download_url image_id:integer

After run the command above, we can see the Rails generate controller, domain

Install and Setup Gem for Mysql

Add mysql2 in Gemfile:

gem 'mysql2'

Then run bundle install to install mysql2 gem

> bundle install

Create and Configure Database

I will use videodb as database name so I need to create videodb schema/database on mysql first then we need to configure correct MySQL Database connection on config/database.yml

Here is my content for config/database.yml file:

default: &default
 adapter: mysql2
 pool: 5
 timeout: 5000
 encoding: utf8
 database: videodb
 pool: 5
 username: yourusername
 password: yourpassword

development:
 <<: *default

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
 <<: *default

production:
 <<: *default

Run DB migration to Create Table

After we have the domain model, install required mysql2 gem and setup database connection correctly we need to run the db migration:

rake db:migrate 

It will show output like this below:

== 20160526014829 CreateVideos: migrating =====================================

-- create_table(:videos)

   -> 0.0291s

== 20160526014829 CreateVideos: migrated (0.0294s) ============================

Create Data Seed

We can populate the database table with initial data by putting the initialization code on db/seeds.rb

Video.create( title: 'My First Video', description: 'My Video Description', language: 'en_US', duration: 600, file_size: 240, share_url: 'http://www.myvideoshare.com/', download_url: 'http://www.mydownloadurl.com/' )

To create the seeds data we can run:

rake db:seed

Run Rails Application
After we have sample data, we can run the Rails app by command:

rails s

As default, rails will run on port 3000, so you can open to http://localhost:3000 at your browser.

And to see the first endpoint we created, you can open to http://localhost:3000/videos to see the list of video as json like this below:

videos_json_list