Rails Routing Roundup
May 17th, 2007 by Lou
David A. Black from Ruby Power and Light gave a tutorial on Rails routing. Rails routes requests based on the URL. This rules based. However, routes are also used to derive URLs from link specifications in views.
- The rails routing mechanism sees incoming URLs, slices, applies rules, and ascertains the response.
- The routing mechanism performs URL generation for views, for example using the link_to method. Routing generates links in views.
Like Janus, the routing mechanism looks both directions. The key artifact for managing routing is config/routes.rb, which is basically a Ruby code block.
There are several types routes or types of routing rules:
- simple routes,
- named routes,
- “resource” or RESTful routes.
One way or the other, Rails will find a controller and an action for every URL request. In Rails, you always need to derive a controller and an action in the URL recognition process.
The tutorial also covered RESTful routing, which specifies a set of conventions for RESTful compliant routing, from which is derived a lot of “in the lagniappe” behavior. The RESTful routing conventions leverage the HTTP request methods to imply another parameter to controller operations. It also specifies a complete set of conventions that covers the 80% solution for managing connections of access to “resources”, particularly for CRUD applications.
The RESTful approach is both a set of theories and postulates about how systems interact, or should interact, as well as a set of conventions and engineering implementations within a Rails application.
I could have gotten a lot more out of this, at least form a Rails coding perspective, if i weren’t such a n00b. A few code examples were given as part of the tutorial. With this, I hope I remember enough of it to follow up later!
The discussions around the somewhat abstract concept of a resource as something that is not directly derived from a Rails model (table) were interesting. In my opinion, a common pitfall in database engineering and information modeling is the chasm between conceptual models that may not map directly to the required implementation models. Leaning how to develop resources and controllers, independent of the Rails generate CRUD table-by-table methods, can present more powerful methods for expressing interactions between systems and between users and systems.