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