Routing Rails

Rails Routing book

David Black, author of the popular Ruby for Rails, has released a guide to using and configuring routes in Ruby on Rails. It’s titled Rails Routing and it is part of Addison-Wesley’s Digital Shortcut series.

In their words:

In this short cut, you’ll learn techniques for writing custom routing rules, how to tap into the convenience and power of named routes, and the workings of the RESTful 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 tips on the use of this important part of your Rails skill set.

While I’m personally looking forward to learning about RESTful routing, the best part about this guide is how it can help beginners get a handle on what routes are, how they work and how to write them. Even though it is an essential concept in Rails, routing has been poorly documented in the past and often comes across as voodoo to beginners who stumble through it with trial-and-error.

There are two ways to get the PDF of Rails Routing. If you are an Addison-Wesley Safari member (or want to be) you can view or download it ($39.99/month) as well as preview portions of the guide. If you’d rather get just the single PDF instead, you can purchase it through their store ($14.99).

2 Responses to “Routing Rails”

  1. Null is Love » Blog Archive » Catch-all Routes Says:

    [...] week, I posted that David Black has released a guide to using and configuring routes in Ruby on Rails and [...]

  2. sohdubom Says:

    hi Kevin … merry xmas … :-) well, i’m studying routes right know, began with simple and named routes and know starting restfull routes. as an experiment i decide to: rake routes , considering my routes.rb file has only: map.resources users. well, we all know the output, but based on this output the last 3 mappings which are: show(get), update(put) and destroy(delete):

    user GET /users/:id {:action=>”show”, :controller=>”users”}
    PUT /users/:id {:action=>”update”, :controller=>”users”}
    DELETE /users/:id {:action=>”destroy”, :controller=>”users”}

    based on the output above i decided to write the equivalent routes in the routes.rb file in order to mimic what map.resources does. so we will have 7 routes. to simplify let’s just consider here these last 3 routes. first, based on the output above, my conclusion is that i could use 3 routes with the same name: map.user and differentiate then with the :method symbol, being get, put and delete for each one of them … so rails would know which one to use based on the :method

    map.user ‘users/:id’, … , :method => :get
    map.user ‘users/:id’, … , :method => :put
    map.user ‘users/:id’, … , :method => :delete

    well, that doesn’t work … if i click delete in the view then the show action will be triggered. my 2nd conclusion is that since i’m not using map.resources and instead i’m using named_routes, then the routing system will use the top-first rule

    my doubt is, how rails understand map.resources since the output from rake routes doesn’t imply us to do something like:

    map.user_show ‘users/:id’, … , :method => :get
    map.user_update ‘users/:id’, … , :method => :put
    map.user_delete ‘users/:id’, … , :method => :delete

    plus if i named_route like the sample above, i don’t need to specify which method to use, cause the routes are named. right?

    then my 3rd conclusion is that rails should understand:

    map.user ‘users/:id’, … , :method => :get
    map.user ‘users/:id’, … , :method => :put
    map.user ‘users/:id’, … , :method => :delete

    based on the :method differentiation. i also did: :requirements => {:method => :delete} in routes.rb or user_path(user) in my view, but no success.

    obs. my delete link_to code:
    link_to ‘delete’, user, :confirm => ‘Confirm delete?’, :method => :delete

    i still haven’t read david black’s book, but i think map.resources does something else behind the curtains.

Leave a Reply