ruby-on-railshtml-tablenested-attributessimple-form-for

How to create a table in simple_form_for with 2 nested attributes and checkboxes


I am trying to create a table to grant a model permissions based on an existing relationship with 2 other models.

The idea is something similar to this: https://ibb.co/DRTywF6

For a given brand I would have:

              |supplier 1 | supplier 2| supplier 3

clothes_type 1     X

clothes_type 2                  X          X

clothes_type 3     X

I created a join table "permissions" and edited the models in order to have access from a brand to both suppliers and clothes_types

Displaying is the headers is fine since I just loop through the array of suppliers but I can't find a way to create checkboxes for each pair of clothes_type/supplier for the brand at hand.

I wrote the following

<%= simple_form_for @brand do |f| %>
  <table class="table table-hover">
    <thead>
      <tr>
        <th nowrap><%= "Item types" %></th>
          <%@suppliers.each do |supplier| %>
            <th nowrap><%= supplier.company %></th>
           <% end %>
      </tr>
    </thead>

    <tbody>

    # that's where I need help :)

    </tbody>

My models are as follow:

class Brand < ActiveRecord::Base
  has_many :permissions
  has_many :suppliers, through: :permissions
  has_many :clothes_types, through: :permissions
end

class Supplier < ActiveRecord::Base
  has_many :permissions
  has_many :brands, through: :permissions
  has_many :clothes_types, through: :permissions
end

class ClothesType < ActiveRecord::Base
  has_many :permissions
  has_many :suppliers, through: :permissions
  has_many :brands, through: :permissions
end

class Permission < ActiveRecord::Base
  belongs_to :supplier
  belongs_to :brand
  belongs_to :clothes_type
end

I tried f.collection_check_boxes but it gives me all the suppliers for a given brand and also does not filter by clothes types.

I would like to be able to display the table for each brand. This table would show for a given clothes_type if you have access or not to the maker. If you do, the checkbox would be checked and if you don't, it would be unchecked, leaving you the option to check it and then submit the form to update the permission.

thanks in advance!


Solution

  • It seems like you would want to do something like:

    <%= simple_form_for @brand do |f| %>
      <table class="table table-hover">
        <thead>
          <tr>
            <th nowrap><%= "Item types" %></th>
            <% @suppliers.each do |supplier| %>
              <th nowrap><%= supplier.company %></th>
            <% end %>
          </tr>
        </thead>
        <tbody>
          <% @clothes_types.each do |clothes_type| %>
            <td nowrap><%= clothes_type.name %></td>
            <% @suppliers.each do |supplier| %>
              <td no_wrap>
                <% if supplier.clothes_types.include?(clothes_type) %>
                  # use check_box_tag here
                <% end %>
              </td>
            <% end %>
          <% end %>
        </tbody>
      </table>
    <% end %>
    

    On that check_box_tag, I'm not sure what you'll want the name to be as your question is somewhat ambiguous. Also, to set the checked value, you'll probably want to do something like:

    supplier.permissions.find_by(clothes_type: clothes_type)
    

    or maybe

    supplier.permissions.find_by(clothes_type: clothes_type).try(:granted?)
    

    It's unclear whether the presence of a permission means the permission is granted or whether, perhaps, the permission has an attribute like granted?. Again, your question is ambiguous.