Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Guide on how to create an API-Only Application with Ruby on Rails 5

Creating an API-Only Application with Ruby on Rails

01 - Create a new API-only Rails app

rails new ror-app-name --api

02 - Basic Scaffold

01 - Model

This step is for creating a very basic level of model for us to work in. If you know already, or wish to apply your own custom models with relationships you can skip this step.

  1. Create an User model
rails generate model User name:string
  1. Create a Post model - association with User
rails generate model Post title:string body:text user:belongs_to
  1. Migrate the models
rails db:migrate

02 - Creating a Dummy Data

Gemfile

group :development do
  gem 'faker'
end

Install gem

bundle install

db/seeds.rb

5.times do
  user = User.create({name: Faker::Name.name})
  user.posts.create({title: Faker::Book.title, body: Faker::Lorem.sentence})
end
rails db:seed

03 - Routes

Nesting the API routes config/routes.rb

namespace 'api' do
    namespace 'v1' do
      resources :posts
      resources :users
    end
  end

Create an api folder inside App/controllers folder. Then create v1 folder inside api folder which you created. Then create controllers. Note - You do not have to create the controllers with rails generate controllers. Create the controllers simply by creating a new file. controllers/api/v1/users_controller.rb

module Api
    module V1
        class UsersController < ApplicationController
        end
    end
end

controllers/api/v1/posts_controller.rb

module Api
    module V1
        class PostsController < ApplicationController
        end
    end
end

04 - Apply CRUD to controllers

controllers/api/v1/posts_controller.rb

module Api
  module V1
    class PostsController < ApplicationController

      def index
        @posts = Post.order('created_at DESC')
        render json: {status: 'SUCCESS', message: 'Loaded posts', data:@posts}, status: :ok
      end

      def show
        @post = Post.find(params[:id])
        render json: {status: 'SUCCESS', message: 'Loaded posts', data:@post}, status: :ok
      end

      def create
        @post = Post.new(post_params)

        if @post.save
          render json: {status: 'SUCCESS', message: 'Post is saved', data:@post}, status: :ok
        else
          render json: {status: 'Error', message: 'Post is not saved', data:@post.errors}, status: :unprocessable_entity
        end
      end

      def update
        @post = Post.find(params[:id])

        if @post.update_attributes(post_params)
          render json: {status: 'SUCCESS', message: 'Post is updated', data:@post}, status: :ok
        else
          render json: {status: 'Error', message: 'Post is not updated', data:@post.errors}, status: :unprocessable_entity
        end
      end

      def destroy
        @post = Post.find(params[:id])
        @post.destroy

        render json: {status: 'SUCCESS', message: 'Post successfully deleted', data:@post}, status: :ok
      end

      private
        def post_params
          params.permit(:title, :body)
        end

    end
  end
end

After updating the code, use software such as Postman to check whether all the routes are working properly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.