RSS

The Reverend

This is my new sandbox... I might pee here, too.
Jun 03
Permalink

Rails, Routes, REST and a resource with a singular name.

So Brad, Mark, and I are sitting at work watching the new Rails Routes Peepcode and someone asked “What happens if you do ‘map.resources :page’ vs. ‘map.resources :pages’”. Be default, we thought Rails would crap out when you did that because page_path would expect an :id, and how would you get the collection (eg. pages_path wouldn’t exist).

After putting map.resources :page into routes.rb and running rake routes, we quickly learned that this has been accounted for with object_index_path… See below:

Rails 2.1 Routes

And further proven in console:

Rails 2.1 Routes

I’m not really sure when this change was implemented, or if it was maybe there all along, but that’s an interesting little tidbit. If you ever need a resource to be name singularly, you’re fine, just use page_index_path for the collection, and then page_path(:id) for the member.

Not bad, Rails Team.

P.S. Brad is the one that caught this in the output from rake routes. Good eye, man!