In the tag index view:
<h1>Listing tags</h1> <table> <tr> <th>Name</th> </tr> <% for tag in @tags %> <tr> <td><%=h tag.name %></td> <td><%= link_to 'Users', user_path(:tag_id=>tag) %></td> <td><%= link_to 'Show', tag_path(tag) %></td> <td><%= link_to 'Edit', edit_tag_path(tag) %></td> <td><%= link_to 'Destroy', tag_path(tag), :confirm => 'Are you sure?', :method => :delete %></td> </tr> <% end %> </table> <br /> <%= link_to 'New tag', new_tag_path %>
Note that
user_path(:tag_id=>tag)translates to
<a href="/users?tag_id=1">Users</a>.
In the controller for users, modify the index and change the standard @users=User.find(:all) to
@tag=Tag.find(params[:tag_id]) if params[:tag_id]
@users = @tag.users || throw rescue User.find(:all)
The
throw rescuewas because the @tag.users could throw an exception as well as having a nil value, and this is DRYer than coding it as
@users = @tag.users || User.find(:all) rescue User.find(:all)
Full details at Displaying a subset of items from an association.