Designing Res Tful Rails Applications

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    Favorites, Groups & Events

    Designing Res Tful Rails Applications - Presentation Transcript

    1. Designing RESTful Rails Applications Obie Fernandez Prepared exclusively for QCon 2007 Copyright 2007 Obie Fernandez (All Rights Reserved)
    2. Topics What is REST? REST in Rails Routing and CRUD Resources and Representations Copyright 2007 Obie Fernandez (All Rights Reserved)
    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. What is REST? Copyright 2007 Obie Fernandez (All Rights Reserved)
    5. Roy Fielding before_destroy DELETE operation after_destroy Copyright 2007 Obie Fernandez (All Rights Reserved)
    6. REST is an “architectural style” manifested in the web Copyright 2007 Obie Fernandez (All Rights Reserved)
    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. REST is designed to help you provide services using the native idioms and constructs of HTTP Copyright 2007 Obie Fernandez (All Rights Reserved)
    9. Just use what HTTP already gives you. Copyright 2007 Obie Fernandez (All Rights Reserved)
    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. REST in Rails view helper methods and enhancements to the routing system Copyright 2007 Obie Fernandez (All Rights Reserved)
    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. Much Rails practice is noncompliant with the precepts of REST from the beginning. Copyright 2007 Obie Fernandez (All Rights Reserved)
    14. Routing and CRUD Copyright 2007 Obie Fernandez (All Rights Reserved)
    15. REST in Rails involves standardization of action names. Copyright 2007 Obie Fernandez (All Rights Reserved)
    16. map.resources :auctions
    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. 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. Opinionated REST Support Copyright 2007 Obie Fernandez (All Rights Reserved)
    20. map.auction ‘auctions/:id’, :controller => “auction”, :action => “show”
    21. <%= link_to item.description, auction_path(item.auction) %>
    22. auction_delete_path
    23. auction_delete_path auction_create_path
    24. auction_create_path
    25. auction_path
    26. auction_path auctions_path
    27. <% form_tag auctions_path do |f| %>
    28. <%= link_to “Click here to view all auctions”, auctions_path %>
    29. http://localhost:3000/auctions Copyright 2007 Obie Fernandez (All Rights Reserved)
    30. The HTTP Verb Copyright 2007 Obie Fernandez (All Rights Reserved)
    31. /auctions submitted in a GET request! /auctions submitted in a POST request! Copyright 2007 Obie Fernandez (All Rights Reserved)
    32. map.resources :auctions
    33. The Standard RESTful Controller Actions Copyright 2007 Obie Fernandez (All Rights Reserved)
    34. index new create show edit update destroy
    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. 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. <%= link_to “Delete this auction”, :url => auction_path(auction), :method => :delete %>
    38. <% form_for “auction”, :url => auction_path(@auction), :html => { :method => :put } do |f| %>
    39. The PUT and DELETE hack
    40. The Special Pairs
    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. <%= link_to “Create a new item”, new_item_path %>
    43. <%= link_to “Edit”, edit_item_path(item) %>
    44. Singular Named Routes Copyright 2007 Obie Fernandez (All Rights Reserved)
    45. map.resource :address_book
    46. Nested Resources Copyright 2007 Obie Fernandez (All Rights Reserved)
    47. /auctions/3/bids/5
    48. /bids/5
    49. /auctions/3/bids/5 params[:auction_id]
    50. map.resources :auctions do |auction| auction.resources :bids end
    51. <%= link_to “See all bids”, auction_bids_path(auction) %>
    52. <%= link_to “See all bids”, auction_bids_path(auction) %> /auctions/3/bids
    53. <%= link_to “See all bids”, auction_bids_path(auction) %> /auctions/3/bids params[:auction_id] # in your controller
    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. map.resources :my_auctions, :controller => :auctions do |auction| auction.resources :my_bids, :controller => :bids end
    56. Restful Route Customizations
    57. map.resources :auctions do |auction| auction.resources :bids end
    58. map.resources :auctions do |auction| auction.resources :bids end /auctions/3/bids/5/retract retract_bid_url(auction, bid)
    59. map.resources :auctions do |auction| auction.resources :bids, :member => { :retract => :get } end
    60. map.resources :auctions do |auction| auction.resources :bids, :member => { :retract => :get } end <%= link_to “Retract”, retract_bid_path(auction, bid) %>
    61. Different Representations of Resources
    62. The responds_to method http://example.com/auctions.xml
    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. <%= link_to “XML version of this auction”, formatted_auction_path(auction, “xml”) %>
    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. Discussion and Q/A
    67. My Book is Shipping!
    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)

    + ConSanFrancisco123ConSanFrancisco123, 6 months ago

    custom

    385 views, 0 favs, 0 embeds more stats

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 385
      • 385 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 0
    • Downloads 5
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories