Rails Routes

Rails routing is a configurable and flexible control-flow mechanism. The app's routes are generally defined in routes.rb, but can be modified (like the rest of Rails) at any time. They define

  • specific mappings, with context-sensitive defaults, from url_for (and related) method calls to URLs (typically within the same app)

  • generic mappings from URLs matching a specified pattern to a designated controller and method (eg, method and view file)

The routing information is available (implicitly) in routes.rb, but obtaining the information from the parse tree is needlessly difficult. The current routing information is available through introspection, so script/console can be used to retrieve the information:

$ script/console
...
rs = ActionController::Routing::Routes; nil
=> nil
puts rs.routes
ANY    /:controller/:action/:id/                {}
ANY    /:controller/:action/:id.:format/        {}
rs.named_routes.each

Resources

  • "Rails Refactoring to Resources: Using CRUD and REST in Your Rails Application";
    Trotter Cashion, in Professional Ruby Collection; Addison-Wesley

    • Chapter 3: Routing
    • Chapter 4: REST, Resources, and Rails
    • Chapter 5: Reflections on Rails Routing


This wiki page is maintained by Rich Morin, an independent consultant specializing in software design, development, and documentation. Please feel free to email comments, inquiries, suggestions, etc!

Topic revision: r8 - 20 Jul 2008, RichMorin
This site is powered by Foswiki Copyright © by the contributing authors. All material on this wiki is the property of the contributing authors.
Foswiki version v2.1.6, Release Foswiki-2.1.6, Plugin API version 2.4
Ideas, requests, problems regarding CFCL Wiki? Send us email