I have 2 tables in my database, Companies & Users. I have one list (displayed as table) which lists out all the companies. Now I want to insert a link in the each row which could take the user to another list (displayed as table) which shows only those users belonging to the particular company row ?
Companies list
<table>
<% @companies.each do |company| %>
<tr>
<td><%= company.Name %></td>
<td><%= company.addressline1 %></td>
<td><%= company.addressline2 %></td>
<td><%= company.City %></td>
<td><%= company.postcode %></td>
<td><%= company.Telephone %></td>
<td><%= link_to 'Edit', edit_company_path(company) %></td>
<td><%= link_to "Users", url_for(:users) %></td>
</tr>
<% end %>
</table>
Users list
<table>
<% @users.each do |user| %>
<tr>
<td><%= user.username %></td>
<td><%= user.email %></td>
<td><%= UserType.find(user.user_type_id).user_type_name %></div>
<td><%= check_box_tag(user, user.is_admin, user.is_admin) %></div>
<td><%= check_box_tag(user, user.active_status, user.active_status) %></div>
<td><%= link_to 'Edit', edit_user_path(user) %></td>
</tr>
<% end %>
There are various ways to do this. You can start by adding a simple route:
And in your
UsersController:And in your companies list view change the link to:
Once you have this in place and working, you may want to consider alternative ways to setup the route. For instance, you might want
usersto be a nested resource undercompanies. See here for more info on this.