Res tful api design & implementation with code igniter php framework_appleboy
Designing Res Tful Rails Applications
-
Upload
consanfrancisco123 -
Category
Business
-
view
843 -
download
1
Transcript of Designing Res Tful Rails Applications
![Page 1: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/1.jpg)
Copyright 2007 Obie Fernandez (All Rights Reserved)
DesigningRESTfulRails ApplicationsObie FernandezPrepared exclusively for QCon 2007
![Page 2: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/2.jpg)
Copyright 2007 Obie Fernandez (All Rights Reserved)
Topics
What is REST?
REST in Rails
Routing and CRUD
Resources and Representations
![Page 3: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/3.jpg)
Copyright 2007 Obie Fernandez (All Rights Reserved)
Presentation Goals
It’s a big topic, so focusing on the most essential stuff
Convince you that REST is worth investigating further
Keep emphasis on practical advice and gotchas
Don’t put you, the audience to sleep
![Page 4: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/4.jpg)
Copyright 2007 Obie Fernandez (All Rights Reserved)
What is REST?
![Page 5: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/5.jpg)
Copyright 2007 Obie Fernandez (All Rights Reserved)
Roy Fielding
before_destroy
DELETE operation
after_destroy
![Page 6: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/6.jpg)
Copyright 2007 Obie Fernandez (All Rights Reserved)
REST is an “architectural style” manifested in the web
![Page 7: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/7.jpg)
Copyright 2007 Obie Fernandez (All Rights Reserved)
The REST constraints include
Use of a client-server architecture
Stateless communication
Explicit signaling of response cacheability
![Page 8: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/8.jpg)
Copyright 2007 Obie Fernandez (All Rights Reserved)
REST is designed to help you provide services using the native idioms and constructs of HTTP
![Page 9: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/9.jpg)
Copyright 2007 Obie Fernandez (All Rights Reserved)
Just use what HTTP already gives you.
![Page 10: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/10.jpg)
Copyright 2007 Obie Fernandez (All Rights Reserved)
One of the payoffs of REST is that it scales relatively well for big systems, like the web.
![Page 11: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/11.jpg)
Copyright 2007 Obie Fernandez (All Rights Reserved)
REST in Railsview helper methods and enhancements to the routing system
![Page 12: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/12.jpg)
Copyright 2007 Obie Fernandez (All Rights Reserved)
Benefits of RESTful Rails
Convenience and automatic best practices for you
A REST interface to your application’s services, for everyone else
![Page 13: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/13.jpg)
Copyright 2007 Obie Fernandez (All Rights Reserved)
Much Rails practice is noncompliant with the precepts of REST from the beginning.
![Page 14: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/14.jpg)
Copyright 2007 Obie Fernandez (All Rights Reserved)
Routing and CRUD
![Page 15: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/15.jpg)
Copyright 2007 Obie Fernandez (All Rights Reserved)
REST in Rails involves standardization of action names.
![Page 16: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/16.jpg)
map.resources :auctions
![Page 17: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/17.jpg)
Copyright 2007 Obie Fernandez (All Rights Reserved)
Resources and Representations
REST characterizes communication between system components as a series of requests to which the responses are representations of resources.
![Page 18: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/18.jpg)
Copyright 2007 Obie Fernandez (All Rights Reserved)
What you actually do get hold of is never the resource itself, but a representation of it.
![Page 19: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/19.jpg)
Copyright 2007 Obie Fernandez (All Rights Reserved)
Opinionated REST Support
![Page 20: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/20.jpg)
map.auction ‘auctions/:id’, :controller => “auction”,
:action => “show”
![Page 21: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/21.jpg)
<%= link_to item.description, auction_path(item.auction) %>
![Page 22: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/22.jpg)
![Page 23: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/23.jpg)
auction_delete_path
![Page 24: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/24.jpg)
auction_delete_path
auction_create_path
![Page 25: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/25.jpg)
auction_create_path
![Page 26: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/26.jpg)
![Page 27: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/27.jpg)
auction_path
![Page 28: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/28.jpg)
auction_path
auctions_path
![Page 29: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/29.jpg)
<% form_tag auctions_path do |f| %>
![Page 30: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/30.jpg)
<%= link_to “Click here to view all auctions”, auctions_path %>
![Page 31: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/31.jpg)
Copyright 2007 Obie Fernandez (All Rights Reserved)
http://localhost:3000/auctions
![Page 32: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/32.jpg)
Copyright 2007 Obie Fernandez (All Rights Reserved)
The HTTP Verb
![Page 33: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/33.jpg)
Copyright 2007 Obie Fernandez (All Rights Reserved)
/auctions submitted in a GET request!
/auctions submitted in a POST request!
![Page 34: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/34.jpg)
map.resources :auctions
![Page 35: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/35.jpg)
Copyright 2007 Obie Fernandez (All Rights Reserved)
The Standard RESTful Controller Actions
![Page 36: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/36.jpg)
indexnew
createshowedit
updatedestroy
![Page 37: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/37.jpg)
Copyright 2007 Obie Fernandez (All Rights Reserved)
A lot of work is done for you and the action names are nicely CRUD-like.
![Page 38: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/38.jpg)
![Page 39: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/39.jpg)
Copyright 2007 Obie Fernandez (All Rights Reserved)
Rules for Request Methods
The default is GET
In a form_tag or form_for call, the POST method will be used automatically
If necessary, you can explicitly specify a request method along with the URL generated by the named route (PUT and DELETE operations)
![Page 40: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/40.jpg)
<%= link_to “Delete this auction”,:url => auction_path(auction),
:method => :delete %>
![Page 41: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/41.jpg)
<% form_for “auction”,:url => auction_path(@auction),
:html => { :method => :put } do |f| %>
![Page 42: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/42.jpg)
The PUT and DELETE hack
![Page 43: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/43.jpg)
The Special Pairs
![Page 44: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/44.jpg)
Copyright 2007 Obie Fernandez (All Rights Reserved)
create and update operations involve two actions
The action that results in the display of the form
The action that processes the form input when the form is created
![Page 45: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/45.jpg)
<%= link_to “Create a new item”, new_item_path %>
![Page 46: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/46.jpg)
<%= link_to “Edit”, edit_item_path(item) %>
![Page 47: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/47.jpg)
Copyright 2007 Obie Fernandez (All Rights Reserved)
Singular Named Routes
![Page 48: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/48.jpg)
map.resource :address_book
![Page 49: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/49.jpg)
Copyright 2007 Obie Fernandez (All Rights Reserved)
Nested Resources
![Page 50: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/50.jpg)
/auctions/3/bids/5
![Page 51: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/51.jpg)
/bids/5
![Page 52: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/52.jpg)
params[:auction_id]
/auctions/3/bids/5
![Page 53: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/53.jpg)
map.resources :auctions do |auction| auction.resources :bids end
![Page 54: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/54.jpg)
<%= link_to “See all bids”, auction_bids_path(auction) %>
![Page 55: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/55.jpg)
<%= link_to “See all bids”, auction_bids_path(auction) %>
/auctions/3/bids
![Page 56: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/56.jpg)
<%= link_to “See all bids”, auction_bids_path(auction) %>
/auctions/3/bids
params[:auction_id] # in your controller
![Page 57: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/57.jpg)
You can nest to any depth.Each level of nesting adds one to the number of arguments you have to supply to the nested routes.
![Page 58: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/58.jpg)
map.resources :my_auctions, :controller => :auctions do |auction| auction.resources :my_bids, :controller => :bids end
![Page 59: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/59.jpg)
Restful Route Customizations
![Page 60: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/60.jpg)
map.resources :auctions do |auction| auction.resources :bids end
![Page 61: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/61.jpg)
map.resources :auctions do |auction| auction.resources :bids end
/auctions/3/bids/5/retract
retract_bid_url(auction, bid)
![Page 62: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/62.jpg)
map.resources :auctions do |auction| auction.resources :bids, :member => { :retract => :get } end
![Page 63: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/63.jpg)
map.resources :auctions do |auction| auction.resources :bids, :member => { :retract => :get } end
<%= link_to “Retract”, retract_bid_path(auction, bid) %>
![Page 64: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/64.jpg)
Different Representations of Resources
![Page 65: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/65.jpg)
The responds_to method
http://example.com/auctions.xml
![Page 66: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/66.jpg)
def index @auctions = Auction.find(:all) respond_to do |format| format.html format.xml { render :xml => @auctions.to_xml } endend
http://example.com/auctions.xml
![Page 67: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/67.jpg)
<%= link_to “XML version of this auction”, formatted_auction_path(auction, “xml”) %>
![Page 68: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/68.jpg)
<%= link_to “XML version of this auction”, formatted_auction_path(auction, “xml”) %>
<a href=”/auctions/1.xml”>XML version of this auction</a>
![Page 69: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/69.jpg)
Discussion and Q/A
![Page 70: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/70.jpg)
My Book is Shipping!
![Page 71: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/71.jpg)
![Page 72: Designing Res Tful Rails Applications](https://reader034.fdocuments.us/reader034/viewer/2022052307/554f6d73b4c9058a148b5056/html5/thumbnails/72.jpg)
Copyright 2007 Obie Fernandez (All Rights Reserved)
My Bloghttp://jroller.com/obie
If you are a Rails developer and you liked this talk, please recommend me on workingwithrails.com