Rails 3 resource routes with dots; or, how to make a Ruby developer go a little bit insane

This one cost me at least an hour of frustration.

So apparently the Rails router has considered the dot (“.”) to be a “separator” character along with the slash (“/”) since version 1.2. I don’t know in what context this ever seemed like a good idea, but whatever. It’s not the sort of thing that’s going to bite you every day, but when it does it will be in very weird ways. To wit:

First, a simple routes.rb.

  resources :users do
    resources :projects

Fill in some typical values, and you get a path:

irb(main):009:0> app.user_projects_path("avdi")
=> "/users/avdi/projects"

Now fill in a value with a period in it, and watch it explode:

irb(main):010:0> app.user_projects_path("avdi.grimm")
ActionController::RoutingError: No route matches {
  :user_id=>"avdi.grimm", :action=>"create", :controller=>"projects"

:action => "create"? What?!! Who said anything about create?!

As it turns out, there is an invocation in your routes file which will fix this:

  resources :users, :constraints => { :id => /.*/ } do
    resources :projects

irb(main):013:0> app.user_projects_path("avdi.grimm")
=> "/users/avdi.grimm/projects"

Now I know what you’re thinking. “That’s so obvious, why didn’t he think of that immediately?” What can I say, some days I’m slow.

UPDATE: Here’s the Rails 2.* version:

  resources :users, :requirements => { :id => /.*/ } do
    resources :projects

And here's a version that accepts anything BUT slashes for the parameter value:

  resources :users, :requirements => { :id => /[^/]+/ } do
    resources :projects