Rails API Application

As a Rails developer, I did not have a chance to create a Rails API application from the scratch. So I thought it would be great experience to build a simple API app.

Rails version 5 supports API only applications so let’s start by creating todo-backend with a magical argument --api.

$ rails _5.1.3_ new todo-backend -T --api
  create  README.md
  create  Rakefile
  create  config.ru
  create  .gitignore
  create  Gemfile
  ...
  • _5.1.3 specifies the Rails version.
  • -T skips the default testing framework.
  • --api tells Rails that we want an API application.

Then the Rails API app is created and is little bit different than a regular Rails app.

Gemfile

  • Jquery and turbolinks gems are removed

application.rb

  • config.api_only = true

application_controller.rb

  • won’t check for CSRF protection
  • inherits from ::API
# before
class ApplicationController < ActionController::Base end

# after
class ApplicationController < ActionController::API end

References

Taeyang Lee

Taeyang Lee
I really enjoy taking on tasks which are out of my comfort zone and using them as a great way to learn the necessary tools to complete it.

Monads

Published on December 17, 2018

Functors

Published on December 16, 2018