Member vs colleciton routes in RoR
Rails resources are fantastic but the docs can be a little lacking in depth. Unfortunately it’s not exactly clear from the docs how you can create a route like /documents/trash without the show route getting in the way.
I recently found myself in the situation where I wanted to create these two routes:
In order to help a user easily restore deleted documents.
My naive implementation was using a member route, take a look:
get '/users/trash' => 'users#trash', as: "users_trash"
get ':user_id' => 'users#show'
resources :user_roles, only: [:new, :destroy, :create]
Now this did function but it was in no way the rails way of doing things. Instead after some reading I learned the right way to do this was collection routes:
resources :users do
Leaving out my irrelevant member routes you can see I've added a collection route. Collection routes are routes that belongs to the entire collection of resources not just one member. Member routes are routes that apply an action to a specific member of the resource.
So next time you need a route inside a resource don't use a one-off use a collection route.