Many to many checkbox form

Every time you will show the form, you should load all permissions as well as preload all permissions in the role you are editing right in your controller.

In your view, you will have to traverse the full list of permissions. Something like this:

<%= for permission <- @permissions do %>
  <input checked="<%= role_has_permission?(@role, permission)" name="role[permissions_ids]" type="checkbox" value="<%= @permission.id %>">
<% end %>

You should probably put the input generation function in your view but that’s the code in rough lines. Keep in mind that params["permissions_ids"] will be nil if no checkbox is checked.

3 Likes