LoginSignup
4
3

More than 5 years have passed since last update.

How to integrate rails-api gem ( Example )

Last updated at Posted at 2016-02-13

Introduction

This note tells you how to use rails-api especially for how to integrate rails-api for existing Rails4 App.

How to

Install Gem

# Gemfile
gem 'rails-api'
gem 'active_model_serializers', github: 'rails-api/active_model_serializers'

then,

bundle install

Make Base Controller for API

# app/controller/api_controller.rb
class ApiController < ActionController::API
end

Make RESTful API controllers

class TestController < ApiController
  # Some CRUD methods here
end

Add Config

# config/application.rb
module App
  class Application < Rails::Application
    config.api_only = false # or true
  end
end

Make Data Serialize

# app/serializers/test_serializer.rb
class TestSerializer < ActiveModel::Serializer
  attributes :id, :something
end

Check

curl -H "Content-Type:application/json; charset=utf-8" \
  -X GET \
  -d '{"someAttribute": "Value"}' \
  http://localhost:3000/test
4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3