This patch adds the ability to specify the name of the :id parameter in the RESTFull routes. We needed this in our current project for two reasons: 1) we needed the :id param to be consistent in nested routes (currently it's either :id of :client_id, depending on the level), and 2) we wanted the ability to change the primary key to one of our choosing, while keeping the controller code readable (for example, ISBN numbers accessed as params[:isbn]).
Adds a parameter named :key to the options to map.resources. If set, the value of this parameter is used instead of :id as the parameter name in all member urls. Also, if the value starts with the singular name of the controller (such as :user_name), then 'user_' will not be added to the prefix_path. Here's an example:
map.resources :clients, :key => :client_name do |client|
client.resources :sites, :key => :name do |site|
site.resources :article, :key => :title
end
end
creates the following urls:
/clients/:client_name
/clients/:client_name/sites/:name
/clients/:client_name/sites/:site_name/articles/:title
Notice that because 'client_name' starts with 'client' (singular version of the controller), the nested path parameter remains 'client_name', and is not automatically set to 'client_client_name'. This example shows the full range of the :key option, but common usage would be more in line with this:
map.resources :users, :key => :user_id do |user|
user.resources :books, :key => :isbn do |book|
book.resources :authors
end
end
/users/2 => /users/:user_id
/users/2/books/0-12-345678-9 => /users/:user_id/books/:isbn
/users/2/books/0-12-345678-9/authors/3 => /users/:user_id/books/:isbn/authors/:id
The attached patch includes relevant tests.