Designing Res Tful Rails Applications

1,062 views

Published on

Published in: Business, Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,062
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
27
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Designing Res Tful Rails Applications

  1. 1. Designing RESTful Rails Applications Obie Fernandez Prepared exclusively for QCon 2007 Copyright 2007 Obie Fernandez (All Rights Reserved)
  2. 2. Topics What is REST? REST in Rails Routing and CRUD Resources and Representations Copyright 2007 Obie Fernandez (All Rights Reserved)
  3. 3. 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 Copyright 2007 Obie Fernandez (All Rights Reserved)
  4. 4. What is REST? Copyright 2007 Obie Fernandez (All Rights Reserved)
  5. 5. Roy Fielding before_destroy DELETE operation after_destroy Copyright 2007 Obie Fernandez (All Rights Reserved)
  6. 6. REST is an “architectural style” manifested in the web Copyright 2007 Obie Fernandez (All Rights Reserved)
  7. 7. The REST constraints include Use of a client-server architecture Stateless communication Explicit signaling of response cacheability Copyright 2007 Obie Fernandez (All Rights Reserved)
  8. 8. REST is designed to help you provide services using the native idioms and constructs of HTTP Copyright 2007 Obie Fernandez (All Rights Reserved)
  9. 9. Just use what HTTP already gives you. Copyright 2007 Obie Fernandez (All Rights Reserved)
  10. 10. One of the payoffs of REST is that it scales relatively well for big systems, like the web. Copyright 2007 Obie Fernandez (All Rights Reserved)
  11. 11. REST in Rails view helper methods and enhancements to the routing system Copyright 2007 Obie Fernandez (All Rights Reserved)
  12. 12. Benefits of RESTful Rails Convenience and automatic best practices for you A REST interface to your application’s services, for everyone else Copyright 2007 Obie Fernandez (All Rights Reserved)
  13. 13. Much Rails practice is noncompliant with the precepts of REST from the beginning. Copyright 2007 Obie Fernandez (All Rights Reserved)
  14. 14. Routing and CRUD Copyright 2007 Obie Fernandez (All Rights Reserved)
  15. 15. REST in Rails involves standardization of action names. Copyright 2007 Obie Fernandez (All Rights Reserved)
  16. 16. map.resources :auctions
  17. 17. Resources and Representations REST characterizes communication between system components as a series of requests to which the responses are representations of resources. Copyright 2007 Obie Fernandez (All Rights Reserved)
  18. 18. What you actually do get hold of is never the resource itself, but a representation of it. Copyright 2007 Obie Fernandez (All Rights Reserved)
  19. 19. Opinionated REST Support Copyright 2007 Obie Fernandez (All Rights Reserved)
  20. 20. map.auction ‘auctions/:id’, :controller => “auction”, :action => “show”
  21. 21. <%= link_to item.description, auction_path(item.auction) %>
  22. 22. auction_delete_path
  23. 23. auction_delete_path auction_create_path
  24. 24. auction_create_path
  25. 25. auction_path
  26. 26. auction_path auctions_path
  27. 27. <% form_tag auctions_path do |f| %>
  28. 28. <%= link_to “Click here to view all auctions”, auctions_path %>
  29. 29. http://localhost:3000/auctions Copyright 2007 Obie Fernandez (All Rights Reserved)
  30. 30. The HTTP Verb Copyright 2007 Obie Fernandez (All Rights Reserved)
  31. 31. /auctions submitted in a GET request! /auctions submitted in a POST request! Copyright 2007 Obie Fernandez (All Rights Reserved)
  32. 32. map.resources :auctions
  33. 33. The Standard RESTful Controller Actions Copyright 2007 Obie Fernandez (All Rights Reserved)
  34. 34. index new create show edit update destroy
  35. 35. A lot of work is done for you and the action names are nicely CRUD-like. Copyright 2007 Obie Fernandez (All Rights Reserved)
  36. 36. 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) Copyright 2007 Obie Fernandez (All Rights Reserved)
  37. 37. <%= link_to “Delete this auction”, :url => auction_path(auction), :method => :delete %>
  38. 38. <% form_for “auction”, :url => auction_path(@auction), :html => { :method => :put } do |f| %>
  39. 39. The PUT and DELETE hack
  40. 40. The Special Pairs
  41. 41. 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 Copyright 2007 Obie Fernandez (All Rights Reserved)
  42. 42. <%= link_to “Create a new item”, new_item_path %>
  43. 43. <%= link_to “Edit”, edit_item_path(item) %>
  44. 44. Singular Named Routes Copyright 2007 Obie Fernandez (All Rights Reserved)
  45. 45. map.resource :address_book
  46. 46. Nested Resources Copyright 2007 Obie Fernandez (All Rights Reserved)
  47. 47. /auctions/3/bids/5
  48. 48. /bids/5
  49. 49. /auctions/3/bids/5 params[:auction_id]
  50. 50. map.resources :auctions do |auction| auction.resources :bids end
  51. 51. <%= link_to “See all bids”, auction_bids_path(auction) %>
  52. 52. <%= link_to “See all bids”, auction_bids_path(auction) %> /auctions/3/bids
  53. 53. <%= link_to “See all bids”, auction_bids_path(auction) %> /auctions/3/bids params[:auction_id] # in your controller
  54. 54. 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.
  55. 55. map.resources :my_auctions, :controller => :auctions do |auction| auction.resources :my_bids, :controller => :bids end
  56. 56. Restful Route Customizations
  57. 57. map.resources :auctions do |auction| auction.resources :bids end
  58. 58. map.resources :auctions do |auction| auction.resources :bids end /auctions/3/bids/5/retract retract_bid_url(auction, bid)
  59. 59. map.resources :auctions do |auction| auction.resources :bids, :member => { :retract => :get } end
  60. 60. map.resources :auctions do |auction| auction.resources :bids, :member => { :retract => :get } end <%= link_to “Retract”, retract_bid_path(auction, bid) %>
  61. 61. Different Representations of Resources
  62. 62. The responds_to method http://example.com/auctions.xml
  63. 63. def index @auctions = Auction.find(:all) respond_to do |format| format.html format.xml { render :xml => @auctions.to_xml } end end http://example.com/auctions.xml
  64. 64. <%= link_to “XML version of this auction”, formatted_auction_path(auction, “xml”) %>
  65. 65. <%= link_to “XML version of this auction”, formatted_auction_path(auction, “xml”) %> <a href=”/auctions/1.xml”>XML version of this auction</a>
  66. 66. Discussion and Q/A
  67. 67. My Book is Shipping!
  68. 68. My Blog http://jroller.com/obie If you are a Rails developer and you liked this talk, please recommend me on workingwithrails.com Copyright 2007 Obie Fernandez (All Rights Reserved)

×