Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Nested REST resources with Rails 1.2

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.

  • Login to see the comments

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 </li></ul><ul><li>DHH's REST keynote (get the slides too!) </li></ul><ul><ul><li> </li></ul></ul><ul><ul><li> </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:// Blog [email_address] Email