rest in rails

28
REST in Rails REST in Rails Chhorn Chamnap Chhorn Chamnap

Upload: chamnap-chhorn

Post on 10-May-2015

1.087 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Rest in Rails

REST in RailsREST in Rails

Chhorn ChamnapChhorn Chamnap

Page 2: Rest in Rails

What is REST?What is REST?

The REST style characterizes communication between system components (where a component is, say, a web browser or a server) as a series of requests to which the responses are representations of resources.

What you actually do get hold of is never the resource itself, but a representation of it.

A resource may, at any given point, be available in any number of representations.

Page 3: Rest in Rails

Resources as URLsResources as URLs

A resource is something like a "business A resource is something like a "business entity" in modeling lingo. It's an entity you entity" in modeling lingo. It's an entity you wish to expose as part of an API. Almost wish to expose as part of an API. Almost always, the entity is a noun, e.g. a person, a always, the entity is a noun, e.g. a person, a car, or a football match.car, or a football match.

Each resource is represented as a unique URL.Each resource is represented as a unique URL.

Page 4: Rest in Rails

Operations as HTTP methodsOperations as HTTP methods

Page 5: Rest in Rails
Page 6: Rest in Rails

Services should be statelessServices should be stateless

An objective of REST is to be able to switch An objective of REST is to be able to switch clients at any time and receive the same result.clients at any time and receive the same result.

So are cookies used at all? Yes, cookies can be So are cookies used at all? Yes, cookies can be used, but mainly for authentication.used, but mainly for authentication.

They should only affect if the user can make a They should only affect if the user can make a call, but not how the server responds.call, but not how the server responds.

Page 7: Rest in Rails

Services should be idempotentServices should be idempotent

"Idempotent" means that once you pass a "Idempotent" means that once you pass a message to service, there's no additional effect message to service, there's no additional effect of passing the same message again.of passing the same message again.

Page 8: Rest in Rails

Benefits of Rails’RESTBenefits of Rails’REST

Convenience and automatic best practices for you

A REST interface to your application’s services, for everyone else

Page 9: Rest in Rails

A Short NoticeA Short Notice

There’s a temptation to call your actions add_item and replace_email_address and things like that. But things get simpler when you name your actions after CRUD operations, or as close to the names of those operations as you can get.

Page 10: Rest in Rails

map.resources :booksmap.resources :books

You will have created four named routes and allow you to connect to seven controller actions.

In most cases, is that they have created a Book model, a book controller with a set of CRUD actions, and some named routes pertaining to that controller (courtesy of map.resources :books).

Page 11: Rest in Rails

:only, :except:only, :except

Each of routes generated by Each of routes generated by map.resourcesmap.resources takes takes up memory in your application, and causes Rails to up memory in your application, and causes Rails to generate additional routing logic.generate additional routing logic.

Luckily, since Rails 2.2 you can use the Luckily, since Rails 2.2 you can use the :only:only and and :except:except options to fine-tune the routes that options to fine-tune the routes that Rails will generate for resources. You can supply a Rails will generate for resources. You can supply a single action, an array of actions, or the special single action, an array of actions, or the special :all:all or or :none:none options. options.

Page 12: Rest in Rails

named routenamed route

Page 13: Rest in Rails

PUT and DELETEPUT and DELETE

A PUT or DELETE request, in the context of REST in Rails, is actually a POST request with a hidden field called _method set to either “put” or “delete”. The Rails application processing the request will pick up on this, and route the request appropriately to the update or destroy action.

Page 14: Rest in Rails

Allows you to introspect all of the routes your application recognizes.

rake routes rake routes commandcommand

Page 15: Rest in Rails

map.resourcemap.resource

Imagine, for instance, that you have a resource that could exist only one, a blog. You don’t need an index action for the blog resource, since there is only (and can only ever be) one blog in the application.

map.resource is designed for just this situation. You use it in just the same way as its plural relative.

Page 16: Rest in Rails

map.resourcemap.resource

map.resource :blog

Page 17: Rest in Rails

map.namespacemap.namespace

In some cases—administration interfaces, for instance—you may need to namespace a resource without actually nesting it under another. For example: /admin/posts

map.namespace :admin do |admin|

map.resources :posts

end

Page 18: Rest in Rails

Nested ResourcesNested Resources /auctions/1/bids/5

map.resources :auctions do |auction|

auction.resources :bids

end auction_bids_url, auction_bids_path, new_auction_bid_url, edit_auction_bid_url

Whenever you use the user named routes, you will provide a group, resource in which they can be nested.

Page 19: Rest in Rails

Nested ResourcesNested Resources <%= link_to “See all bids”, auction_bids_path(@auction) %>

<%= link_to “Delete this bid”, auction_bid_path(@auction, @bid), :method => :delete %>

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 20: Rest in Rails

Deep Nesting?Deep Nesting?

Resources should never be nested more than one level deep.

map.resources :auctions do |auctions|

auctions.resources :bids do |bids|

bids.resources :comments

end

end

Page 21: Rest in Rails

Deep Nesting? (cont.)Deep Nesting? (cont.) map.resources :auctions do |auctions|

auctions.resources :bidsendmap.resources :bids do |bids|

bids.resources :commentsendmap.resources :comments

auctions_path # /auctions auctions_path(1) # /auctions/1 auction_bids_path(1) # /auctions/1/bids bid_path(2) # /bids/2 bid_comments_path(3) # /bids/3/comments comment_path(4) # /comments/4

Page 22: Rest in Rails

:path_prefix:path_prefix map.resources :auctions map.resources :bids, :path_prefix => “auctions/:auction_id”

What you’re saying here is that you want all of the bids URLs to include the static string “auctions” and a value for auction_id.

The main difference has to do with the naming of the helper methods that are generated. Nested resources automatically get a name prefix corresponding to their parent resource.

Page 23: Rest in Rails

:name_prefixname_prefix

/auctions/2/bids/5 /auctions/5 map.resources :auctions do |auction|

auction.resources :bids, :name_prefix => nil

end

auction_path(@auction, @bid) auction_path(@auction)

Page 24: Rest in Rails

RESTful ControllersRESTful Controllers It was just presented as something that happens

automatically, which in fact it does, based on the name of the resource.

map.resources :auctions do |auction|

auction.resources :bids

end Having the option means you can name the

(userfacing) resource whatever you want, and keep the name of your controller aligned with different naming standards

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

end

Page 25: Rest in Rails

Extra Member RoutesExtra Member Routes

For example, let’s say we want to make it possible to retract a bid.

map.resources :auctions do |auction|

auction.resources :bids

end retract_bid_url map.resources :auctions do |a|

a.resources :bids, :member => {:retract => :any}

end

Page 26: Rest in Rails

Extra Collection RoutesExtra Collection Routes

You can also use this routing technique to add routes that conceptually apply to an entire collection of resources

map.resources :auctions, :collection => { :terminate => :any }

Page 27: Rest in Rails

Doing it yourselfDoing it yourself

There are two main circumstances when you might want to hand-code your RESTful routes instead of using the macros: when your application does not expose the full set

of actions, and when your URIs follow a nonstandard pattern.

Page 28: Rest in Rails

Doing it yourselfDoing it yourself