+ All Categories
Home > Business > Designing Res Tful Rails Applications

Designing Res Tful Rails Applications

Date post: 10-May-2015
Category:
Upload: consanfrancisco123
View: 843 times
Download: 1 times
Share this document with a friend
Popular Tags:
72
Copyright 2007 Obie Fernandez (All Rights Reserved) Designing RESTful Rails Applications Obie Fernandez Prepared exclusively for QCon 2007
Transcript
Page 1: Designing Res Tful Rails Applications

Copyright 2007 Obie Fernandez (All Rights Reserved)

DesigningRESTfulRails ApplicationsObie FernandezPrepared exclusively for QCon 2007

Page 2: Designing Res Tful Rails Applications

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

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

Copyright 2007 Obie Fernandez (All Rights Reserved)

What is REST?

Page 5: Designing Res Tful Rails Applications

Copyright 2007 Obie Fernandez (All Rights Reserved)

Roy Fielding

before_destroy

DELETE operation

after_destroy

Page 6: Designing Res Tful Rails Applications

Copyright 2007 Obie Fernandez (All Rights Reserved)

REST is an “architectural style” manifested in the web

Page 7: Designing Res Tful Rails Applications

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

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

Copyright 2007 Obie Fernandez (All Rights Reserved)

Just use what HTTP already gives you.

Page 10: Designing Res Tful Rails Applications

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

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

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

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

Copyright 2007 Obie Fernandez (All Rights Reserved)

Routing and CRUD

Page 15: Designing Res Tful Rails Applications

Copyright 2007 Obie Fernandez (All Rights Reserved)

REST in Rails involves standardization of action names.

Page 16: Designing Res Tful Rails Applications

map.resources :auctions

Page 17: Designing Res Tful Rails Applications

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

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

Copyright 2007 Obie Fernandez (All Rights Reserved)

Opinionated REST Support

Page 20: Designing Res Tful Rails Applications

map.auction ‘auctions/:id’, :controller => “auction”,

:action => “show”

Page 21: Designing Res Tful Rails Applications

<%= link_to item.description, auction_path(item.auction) %>

Page 22: Designing Res Tful Rails Applications
Page 23: Designing Res Tful Rails Applications

auction_delete_path

Page 24: Designing Res Tful Rails Applications

auction_delete_path

auction_create_path

Page 25: Designing Res Tful Rails Applications

auction_create_path

Page 26: Designing Res Tful Rails Applications
Page 27: Designing Res Tful Rails Applications

auction_path

Page 28: Designing Res Tful Rails Applications

auction_path

auctions_path

Page 29: Designing Res Tful Rails Applications

<% form_tag auctions_path do |f| %>

Page 30: Designing Res Tful Rails Applications

<%= link_to “Click here to view all auctions”, auctions_path %>

Page 31: Designing Res Tful Rails Applications

Copyright 2007 Obie Fernandez (All Rights Reserved)

http://localhost:3000/auctions

Page 32: Designing Res Tful Rails Applications

Copyright 2007 Obie Fernandez (All Rights Reserved)

The HTTP Verb

Page 33: Designing Res Tful Rails Applications

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

map.resources :auctions

Page 35: Designing Res Tful Rails Applications

Copyright 2007 Obie Fernandez (All Rights Reserved)

The Standard RESTful Controller Actions

Page 36: Designing Res Tful Rails Applications

indexnew

createshowedit

updatedestroy

Page 37: Designing Res Tful Rails Applications

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
Page 39: Designing Res Tful Rails Applications

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

<%= link_to “Delete this auction”,:url => auction_path(auction),

:method => :delete %>

Page 41: Designing Res Tful Rails Applications

<% form_for “auction”,:url => auction_path(@auction),

:html => { :method => :put } do |f| %>

Page 42: Designing Res Tful Rails Applications

The PUT and DELETE hack

Page 43: Designing Res Tful Rails Applications

The Special Pairs

Page 44: Designing Res Tful Rails Applications

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

<%= link_to “Create a new item”, new_item_path %>

Page 46: Designing Res Tful Rails Applications

<%= link_to “Edit”, edit_item_path(item) %>

Page 47: Designing Res Tful Rails Applications

Copyright 2007 Obie Fernandez (All Rights Reserved)

Singular Named Routes

Page 48: Designing Res Tful Rails Applications

map.resource :address_book

Page 49: Designing Res Tful Rails Applications

Copyright 2007 Obie Fernandez (All Rights Reserved)

Nested Resources

Page 50: Designing Res Tful Rails Applications

/auctions/3/bids/5

Page 51: Designing Res Tful Rails Applications

/bids/5

Page 52: Designing Res Tful Rails Applications

params[:auction_id]

/auctions/3/bids/5

Page 53: Designing Res Tful Rails Applications

map.resources :auctions do |auction| auction.resources :bids end

Page 54: Designing Res Tful Rails Applications

<%= link_to “See all bids”, auction_bids_path(auction) %>

Page 55: Designing Res Tful Rails Applications

<%= link_to “See all bids”, auction_bids_path(auction) %>

/auctions/3/bids

Page 56: Designing Res Tful Rails Applications

<%= 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

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

map.resources :my_auctions, :controller => :auctions do |auction| auction.resources :my_bids, :controller => :bids end

Page 59: Designing Res Tful Rails Applications

Restful Route Customizations

Page 60: Designing Res Tful Rails Applications

map.resources :auctions do |auction| auction.resources :bids end

Page 61: Designing Res Tful Rails Applications

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

map.resources :auctions do |auction| auction.resources :bids, :member => { :retract => :get } end

Page 63: Designing Res Tful Rails Applications

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

Different Representations of Resources

Page 65: Designing Res Tful Rails Applications

The responds_to method

http://example.com/auctions.xml

Page 66: Designing Res Tful Rails Applications

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

<%= link_to “XML version of this auction”, formatted_auction_path(auction, “xml”) %>

Page 68: Designing Res Tful Rails Applications

<%= 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

Discussion and Q/A

Page 70: Designing Res Tful Rails Applications

My Book is Shipping!

Page 71: Designing Res Tful Rails Applications
Page 72: Designing Res Tful Rails Applications

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


Recommended