To set up nested resources in Rails, I have seen example routes given like this:
map.resources :players
map.resources :teams, :has_many => :players
By doing this, you can visit teams/1/players and see a list. But it lists all players, not just those that belong to team 1.
How can I list only the resources that are associated with the parent resource?
You need to load the team first. A common practice is to do this in a before filter.
Note that the code above insists that you specify a team. If you want the same controller to work for both, you need to change it slightly (i.e. check for
params[:team_id]).You can use the excellent inherited_resources gem to DRY this up if you controller logic is straightforward.