Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up
for free
Rails API
Saurabh Bhatia
February 22, 2014
Technology
0
400
Rails API
API only applications with Rails API
Saurabh Bhatia
February 22, 2014
Tweet
Share
More Decks by Saurabh Bhatia
See All by Saurabh Bhatia
thewub
0
370
thewub
0
350
thewub
0
30
thewub
1
360
Other Decks in Technology
See All in Technology
ch1aki
2
240
dena_tech
0
170
miura55
0
360
piazza
0
110
dena_tech
1
180
soracom
0
180
yunoda
0
140
hagyyyy
1
200
ayatokura
0
100
ihcomega56
1
600
robcrowley
0
160
sadayoshitada0919
0
370
Featured
See All Featured
samlambert
237
10k
lynnandtonic
272
16k
bkeepers
321
53k
sachag
267
17k
colly
188
14k
afnizarnur
176
14k
eileencodes
113
25k
swwweet
206
6.9k
rmw
11
810
marcelosomers
221
15k
bermonpainter
342
26k
dougneiner
118
7.9k
Transcript
API Only Applications with rails-api Saurabh Bhatia http://fedible.org
what? $ gem install rails-api Rails minus some middleware stack,
actionpack modules and template generation.
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.
Generating a new app $ rails-api new notesapp
No views
rails-api controller app/controllers/application_controller.rb class ApplicationController < ActionController::API end
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.
extending controller class ApplicationController < ActionController::API include ActionController::MimeResponds include ActionController::ImplicitRender
end • MimeResponds and Implicit render provide support for respond_to, respond_with.
middleware
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
extended
serializers • gem "active_model_serializers" • $rails g serialzer post •
$ ls assets controllers mailers models serializers app$ cd serializers/ app/serializers$ ls post_serializer.rb
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"}] }
Shameless Plug : Rails 4 application development
Thank you 謝謝