Upload
best-tech-videos
View
220
Download
0
Embed Size (px)
Citation preview
8/14/2019 Designing RESTful Rails Applications
1/72
DesigningRESTful
Rails
A licationsObie Fernandez
Prepared exclusively for QCon 2007
8/14/2019 Designing RESTful Rails Applications
2/72
Topics
What is REST?
REST in Rails
Routing and CRUD
Resources and Representations
8/14/2019 Designing RESTful Rails Applications
3/72
Presentation Goals
Its 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
Dont put you, the audience to sleep
8/14/2019 Designing RESTful Rails Applications
4/72
What is REST?
8/14/2019 Designing RESTful Rails Applications
5/72
Roy Fielding
before_destroy
DELETE operation
after_destroy
8/14/2019 Designing RESTful Rails Applications
6/72
REST is an architecturalstyle manifested in the web
8/14/2019 Designing RESTful Rails Applications
7/72
The REST
constraints includeUse of a client-server architectureStateless communication
Explicit signaling of responsecacheability
8/14/2019 Designing RESTful Rails Applications
8/72
REST is designed to help
you provide services usingthe native idioms and
constructs of HTTP
8/14/2019 Designing RESTful Rails Applications
9/72
Just use what HTTPalready gives you.
8/14/2019 Designing RESTful Rails Applications
10/72
One of the payoffs of REST is
that it scales relatively well for
big systems, like the web.
8/14/2019 Designing RESTful Rails Applications
11/72
REST in Railsview helper methods and enhancements tothe routing system
8/14/2019 Designing RESTful Rails Applications
12/72
Benefits of RESTful Rails
Convenience and
automatic best practices
for you
A REST interface to your
applications services, for
everyone else
8/14/2019 Designing RESTful Rails Applications
13/72
Much Rails practice is
noncompliant with theprecepts of REST
from the beginning.
8/14/2019 Designing RESTful Rails Applications
14/72
Routing and CRUD
8/14/2019 Designing RESTful Rails Applications
15/72
REST in Rails involves
standardization of actionnames.
8/14/2019 Designing RESTful Rails Applications
16/72
map.resources :auctions
8/14/2019 Designing RESTful Rails Applications
17/72
Resources and
RepresentationsREST characterizes communication between
system components as a series of requests to
which the responses are representations ofresources.
8/14/2019 Designing RESTful Rails Applications
18/72
What you actually do get hold
of is never the resource itself,but a representation of it.
8/14/2019 Designing RESTful Rails Applications
19/72
OpinionatedREST Support
8/14/2019 Designing RESTful Rails Applications
20/72
map.auction auctions/:id,:controller => auction,:action => show
8/14/2019 Designing RESTful Rails Applications
21/72
8/14/2019 Designing RESTful Rails Applications
22/72
8/14/2019 Designing RESTful Rails Applications
23/72
auction_delete_path
8/14/2019 Designing RESTful Rails Applications
24/72
auction_delete_path
auction_create_path
8/14/2019 Designing RESTful Rails Applications
25/72
auction_create_path
8/14/2019 Designing RESTful Rails Applications
26/72
8/14/2019 Designing RESTful Rails Applications
27/72
auction_path
8/14/2019 Designing RESTful Rails Applications
28/72
auction_path
auctions_path
8/14/2019 Designing RESTful Rails Applications
29/72
8/14/2019 Designing RESTful Rails Applications
30/72
8/14/2019 Designing RESTful Rails Applications
31/72
http://localhost:3000/auctionshttp://localhost:3000/auctions8/14/2019 Designing RESTful Rails Applications
32/72
The HTTP Verb
8/14/2019 Designing RESTful Rails Applications
33/72
/auctions submitted in a GET request!
/auctions submitted in a POST request!
8/14/2019 Designing RESTful Rails Applications
34/72
map.resources :auctions
8/14/2019 Designing RESTful Rails Applications
35/72
The Standard RESTfulController Actions
8/14/2019 Designing RESTful Rails Applications
36/72
index
new
create
showedit
updatedestroy
8/14/2019 Designing RESTful Rails Applications
37/72
A lot of work is done for you
and the action names arenicely CRUD-like.
8/14/2019 Designing RESTful Rails Applications
38/72
8/14/2019 Designing RESTful Rails Applications
39/72
Rules for Request Methods
The default is GET
In a form_tag or form_for call, the POST methodwill be used automatically
If necessary, you can explicitly specify a request
method along with the URL generated by the namedroute (PUT and DELETE operations)
8/14/2019 Designing RESTful Rails Applications
40/72
auction_path(auction),:method => :delete %>
8/14/2019 Designing RESTful Rails Applications
41/72
auction_path(@auction),:html => { :method => :put } do |f| %>
8/14/2019 Designing RESTful Rails Applications
42/72
The PUT and DELETE hack
8/14/2019 Designing RESTful Rails Applications
43/72
The Special Pairs
8/14/2019 Designing RESTful Rails Applications
44/72
create and update
operations involve twoactions
The action that results in thedisplay of the form
The action that processes the forminput when the form is created
8/14/2019 Designing RESTful Rails Applications
45/72
8/14/2019 Designing RESTful Rails Applications
46/72
8/14/2019 Designing RESTful Rails Applications
47/72
Singular Named Routes
8/14/2019 Designing RESTful Rails Applications
48/72
map.resource :address_book
8/14/2019 Designing RESTful Rails Applications
49/72
Nested Resources
8/14/2019 Designing RESTful Rails Applications
50/72
/auctions/3/bids/5
8/14/2019 Designing RESTful Rails Applications
51/72
/bids/5
8/14/2019 Designing RESTful Rails Applications
52/72
params[:auction_id]
/auctions/3/bids/5
8/14/2019 Designing RESTful Rails Applications
53/72
map.resources :auctionsdo |auction|
auction.resources :bidsend
8/14/2019 Designing RESTful Rails Applications
54/72
8/14/2019 Designing RESTful Rails Applications
55/72
/auctions/3/bids
8/14/2019 Designing RESTful Rails Applications
56/72
/auctions/3/bids
params[:auction_id] # in your controller
8/14/2019 Designing RESTful Rails Applications
57/72
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.
8/14/2019 Designing RESTful Rails Applications
58/72
map.resources :my_auctions, :controller => :auctionsdo |auction|auction.resources :my_bids, :controller => :bids
end
8/14/2019 Designing RESTful Rails Applications
59/72
Restful Route Customizations
8/14/2019 Designing RESTful Rails Applications
60/72
map.resources :auctionsdo |auction|auction.resources :bids
end
8/14/2019 Designing RESTful Rails Applications
61/72
map.resources :auctionsdo |auction|auction.resources :bids
end
/auctions/3/bids/5/retract
retract_bid_url(auction, bid)
8/14/2019 Designing RESTful Rails Applications
62/72
map.resources :auctionsdo |auction|auction.resources :bids,
:member => { :retract => :get }
end
8/14/2019 Designing RESTful Rails Applications
63/72
map.resources :auctionsdo |auction|auction.resources :bids,
:member => { :retract => :get }
end
8/14/2019 Designing RESTful Rails Applications
64/72
Different Representations ofResources
8/14/2019 Designing RESTful Rails Applications
65/72
The responds_to method
http://example.com/auctions.xml
http://example.com/auctions.xmlhttp://example.com/auctions.xmlhttp://example.com/auctions.xmlhttp://example.com/auctions.xml8/14/2019 Designing RESTful Rails Applications
66/72
defindex@auctions = Auction.find(:all)respond_todo |format|
format.htmlformat.xml {
render :xml => @auctions.to_xml}
endend
http://example.com/auctions.xml
http://example.com/auctions.xmlhttp://example.com/auctions.xmlhttp://example.com/auctions.xmlhttp://example.com/auctions.xmlhttp://example.com/auctions.xml8/14/2019 Designing RESTful Rails Applications
67/72
8/14/2019 Designing RESTful Rails Applications
68/72
XML version of this auction
8/14/2019 Designing RESTful Rails Applications
69/72
Discussion and Q/A
8/14/2019 Designing RESTful Rails Applications
70/72
My Book is
Shipping!
8/14/2019 Designing RESTful Rails Applications
71/72
8/14/2019 Designing RESTful Rails Applications
72/72
My Blog
If you are a Rails developer and you
liked this talk, please recommend
me on workingwithrails.com
http://jroller.com/obiehttp://jroller.com/obie