I really feel like this should be in the Rails Routing from Outside In guide. It took me 6 months and 4 projects to “accidentally” stumble upon this feature I never knew existed, but would have made my life a heck of a lot easier for those projects:
A common use case is this: consider a Profile model. When a user is logged in, you only want him to be able to view his own profile in full. Naturally, this is the ideal use case for singleton resource. However, for singleton resources, you can never get paths which take an id, even if you declare an action in the member scope. So if you wanted to implement an admin module to search on those profiles and preview one of them, you’d have to create a ‘get preview’ form with id as a parameter. This is what the guide led me to believe.
There is absolutely no mention in the guide that singleton and normal resources can coexist. I used to think ‘resource :profile’ and ‘resources :profiles’ would route to different controllers. Once you realize they can go together, the perfect solution would be to have a ‘resource profile’ containing show, and a ‘resources :profiles’ containing search and preview.
I feel it would be greatly helpful to newcomers if this use case was described in the guide. It would also hopefully stop the frequent questions of ‘why am i getting dots in path helper’ when they try to pass an object to the path helper of a singleton resource.
What do you guys think about this?