In my rails app I have a table called Permissions which has the following columns:
id
name
controller
action
description
I also have a Role, which has an association with each of these Permissions.
When editing a Role, I want to be able to select which of the Permissions are allowed for that Role in the form of checkboxes. However I want to group each of the Permissions by whatever the controller name is.
This is what I have in my view:
<table>
<% for permission in Permission.all %>
<tr>
<th rowspan="2"><%= permission.controller %></th>
<% for permission in Permission.all %>
<th style="text-align: center;"><%= permission.action %></th>
<% end %>
</tr>
<tr>
<% for permission in Permission.all %>
<td style="text-align: center;"><%= check_box_tag "role[permission_ids][]", permission.id, @role.permissions.include?(permission) %></td>
<% end %>
</tr>
<% end %>
</table>
So as you can see I basically loop through all of the Permissions to create a table row that contains the controller name and all of the actions. I then create another row with all of the checkboxes for each of those actions.
What I want to do is make it so that my loop is for each Permission controller and then right out the permissions that have the same controller.