Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rails API

Rails API

API only applications with Rails API

Saurabh Bhatia

February 22, 2014
Tweet

More Decks by Saurabh Bhatia

Other Decks in Technology

Transcript

  1. what? $ gem install rails-api Rails minus some middleware stack,

    actionpack modules and template generation.
  2. why? • Lightweight • Fast • API only application –

    backend for mobile app, web application is just another client (e.g. html, angular.js ) to a json api.
  3. controller actions def index @posts = Post.all render json: @posts

    end • rails-api uses render instead of respond methods. • Let's see how to include those.
  4. extending middleware $config/application.rb require File.expand_path('../boot', __FILE__) require 'rails/all'. Bundler.require(:default, Rails.env)

    module Notesapp class Application < Rails::Application config.middleware.use ActionDispatch::Flash end end
  5. serializers • gem "active_model_serializers" • $rails g serialzer post •

    $ ls assets controllers mailers models serializers app$ cd serializers/ app/serializers$ ls post_serializer.rb
  6. serialzers class PostSerializer < ActiveModel::Serializer attributes :title, :body end •

    JSON Response {"posts": [{"id":1,"title":"First Post","body":"Test"}, {"id":2,"title":"Second Post","body":"Another Test"}] }