Rails Routing

Book description

The Rails routing system has always been a key component of theframework, and with the introduction of RESTful routes in the pastyear, it has taken center stage. Fully programmable, the routing systemgoverns the process of mapping request URLs to the appropriatecontroller action. Routing also helps you when you’re writing code,especially view templates, where the same logic that interprets URLsworks in reverse to automate the generation of URL strings in yourcode. As all routing is rule-based, you have to know how to write therules to get the most out of the routing system. In this short cut, you’lllearn techniques for writing custom routing rules, how to tap into theconvenience and power of named routes, and the workings of theRESTful routing that’s had such an impact on Rails development.

Along with a thorough introduction to routing syntax and semantics,you’ll find techniques for testing and troubleshooting routes, and tipson the use of this important part of your Rails skill set.

Section 1: What This Short Cut Covers 3

Section 2: Introduction 6

Section 3: The Routing System 13

Section 4: Writing Custom Routes 31

Section 5: Named Routes 45

Section 6: REST, Resources, Representations, Routing, and Ruby on Rails 53

Section 7: Reflecting on Routes 91

Further Resources 112

Acknowledgments 113

About the Author 114

Table of contents

  1. Copyright
  2. 1. What This Short Cut Covers
    1. 1.1. Preface and Road Map
      1. 1.1.1. The Sample Application
      2. 1.1.2. Software Versions
  3. 2. Introduction
    1. 2.1. Routing and the Dispatcher
    2. 2.2. Up Close and Personal with the Dispatcher
  4. 3. The Routing System
    1. 3.1. The Two Faces of Routing
      1. 3.1.1. Bound Parameters
        1. Recognition
        2. Generation
      2. 3.1.2. Wildcard Components (“Receptors”)
        1. Recognition
        2. Generation
      3. 3.1.3. Static Strings
        1. Recognition
        2. Generation
    2. 3.2. The routes.rb File
    3. 3.3. The Default Route
      1. 3.3.1. Default Route Recognition
      2. 3.3.2. Spotlight on the :id Field
      3. 3.3.3. Default Route Generation
      4. 3.3.4. Modifying the Default Route
    4. 3.4. The Ante-Default Route and respond_to
      1. 3.4.1. respond_to and the HTTP-Accept Header
    5. 3.5. The Empty Route
  5. 4. Writing Custom Routes
    1. 4.1. Using Static Strings
    2. 4.2. Using Your Own “Receptors”
    3. 4.3. A Note on Route Order
    4. 4.4. Using Regular Expressions in Routes
    5. 4.5. Default Parameters and the url_for Method
      1. 4.5.1. What Happened to :id?
    6. 4.6. Using Literal URLs
    7. 4.7. Route Globbing
    8. 4.8. Globbing Auction Item Descriptions
  6. 5. Named Routes
    1. 5.1. _path versus _url
    2. 5.2. What to Name Your Routes
      1. 5.2.1. A Bit of Argument Sugar
      2. 5.2.2. A Little Sugar with Your Sugar?
    3. 5.3. The Special Scope Method, with_options
  7. 6. REST, Resources, Representations, Routing, and Ruby on Rails
    1. 6.1. REST in a Rather Small Nutshell
    2. 6.2. REST in Rails
    3. 6.3. Routing and CRUD
    4. 6.4. Resources and Representations
      1. 6.4.1. REST Resources and Rails
    5. 6.5. From Named Routes to REST Support
    6. 6.6. Reenter the HTTP Verb
    7. 6.7. The Standard RESTful Controller Actions
      1. 6.7.1. The PUT and DELETE Cheat
    8. 6.8. Singular and Plural RESTful Routes
    9. 6.9. The Special Pairs: new/create and edit/update
    10. 6.10. Nested Resources
      1. 6.10.1. To Nest or Not to Nest?
      2. 6.10.2. Another Route to Nesting: path_prefix
      3. 6.10.3. Namespacing RESTful Routes with :name_prefix
    11. 6.11. Adding Custom Actions with :member and :collection
    12. 6.12. Controller-Only Resources
    13. 6.13. Singleton Resource Routes
    14. 6.14. Returning Different Representations
      1. 6.14.1. Formatted Named Routes
    15. 6.15. At Home with the Rails REST Action Set
      1. 6.15.1. Index
      2. 6.15.2. Show
      3. 6.15.3. Destroy
      4. 6.15.4. New and Create
      5. 6.15.5. Edit and Update
  8. 7. Reflecting on Routes
    1. 7.1. Examining Routes in the Application Console
      1. 7.1.1. Dumping Routes
      2. 7.1.2. Anatomy of a Route Object
      3. 7.1.3. Recognition and Generation in the Console
      4. 7.1.4. Named Routes in the Console
    2. 7.2. Testing Routes
      1. 7.2.1. A Word about Argument Syntax
    3. 7.3. The Routing Navigator Plug-in
  9. Further Resources
  10. Acknowledgments
  11. About the Author
  12. Also Available in The Addison-Wesley Professional Ruby Series

Product information

  • Title: Rails Routing
  • Author(s): David A. Black
  • Release date: May 2007
  • Publisher(s): Addison-Wesley Professional
  • ISBN: None