Nested REST resources with Rails 1.2

4,276 views

Published on

A presentation on RESTful routing and resources in Rails 1.2. I stepped through some code to show how to get started with the scaffold_resource generator, set up nested resources using REST, use the new named routes, and use respond_to to return information in different formats (HTML/XML/JS/JSON). I also talked about the motivations behind RESTful design, and concrete advantages to embracing REST.

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

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

No notes for slide

Nested REST resources with Rails 1.2

  1. 1. RESTful Routing Basics RoR Meetup, 1/18/2007 Andre Lewis
  2. 2. What You’ll Learn <ul><li>Getting started with RESTful resources in Rails 1.2 </li></ul><ul><li>Using scaffold_resource </li></ul><ul><li>How to nest resources </li></ul>
  3. 3. Motivation <ul><li>Why care about REST? </li></ul><ul><li>What can REST do for you? </li></ul>
  4. 4. … <ul><li>Fewer decisions to make </li></ul><ul><li>More easily understand other’s work, or your own older work </li></ul><ul><li>Nice URLs </li></ul><ul><li>Convenient named routes </li></ul><ul><li>XML/JS/JSON responses, easier </li></ul>
  5. 5. The REST Mindset <ul><li>Think in terms of: </li></ul><ul><ul><li>C reate  GET /items/new </li></ul></ul><ul><ul><li>R etrieve  GET /items/1 </li></ul></ul><ul><ul><li>U pdate  PUT /items/1 </li></ul></ul><ul><ul><li>D elete  PUT /items/1;delete </li></ul></ul><ul><li>Let the constraints imposed by CRUD guide your controller design </li></ul>
  6. 6. Let’s code a Nested Resource
  7. 7. Review: Nested Resources <ul><li>Create the resource scaffolding and migrations </li></ul><ul><li>Set up relationships in models </li></ul><ul><li>Modify routes.rb </li></ul><ul><li>Modify the controller of the nested resource </li></ul><ul><ul><li>add a before_filter </li></ul></ul><ul><ul><li>amend the *_url calls in create and update actions </li></ul></ul><ul><ul><li>scope your Comment (nested class) finders to the Post (nestee class) </li></ul></ul><ul><li>Modify the views </li></ul><ul><li>Provide a link to the nested resource </li></ul>
  8. 8. REST: Learn More <ul><li>My blog post at http://earthcode.com </li></ul><ul><li>DHH's REST keynote (get the slides too!) </li></ul><ul><ul><li>http://www.loudthinking.com/lt-files/worldofresources.pdf </li></ul></ul><ul><ul><li>http://www.scribemedia.org/2006/07/09/dhh/ </li></ul></ul><ul><li>The 2nd edition of Pragmatic's Agile Web Development </li></ul><ul><li>Peepcode screencast and cheatsheet </li></ul>Questions/Feedback? <ul><li>what about *optionally* nested resources? </li></ul><ul><li>What about the new/edit request.get? pattern? </li></ul><ul><li>Is it “bad” to put additional methods on your RESTful controllers? </li></ul>
  9. 9. My Info Book http:// earthcode.com Blog [email_address] Email

×