我正在尝试创建一个表格,以基于与其他2个模型的现有关系授予模型权限。
这个想法与此类似:https://ibb.co/drtywf6
对于给定品牌,我将拥有:
|supplier 1 | supplier 2| supplier 3
clothes_type 1 X
clothes_type 2 X X
clothes_type 3 X
我创建了一个加入表"权限",并编辑了模型,以便可以从品牌访问供应商和ducker_types
显示为标题很好,因为我只是循环浏览各种供应商,但我找不到一种为手头品牌的每对dacker_type/供应商创建复选框的方法。
我写了以下
<%= 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>
我的模型如下:
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
我尝试了f.collection_check_boxes
,但它为我提供了给定品牌的所有供应商,也不会因衣服类型而过滤。
我希望能够为每个品牌展示桌子。如果您可以访问或不访问制造商,则该表将显示给定的Charter_Type。如果您这样做,将检查复选框,如果您不进行检查,则将不受控制,使您可以选择检查它,然后提交表格以更新许可。
预先感谢!
似乎您想做这样的事情:
<%= 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 %>
在该check_box_tag上,我不确定您希望name
是什么,因为您的问题有些模棱两可。另外,要设置checked
值,您可能想做以下操作:
supplier.permissions.find_by(clothes_type: clothes_type)
或也许
supplier.permissions.find_by(clothes_type: clothes_type).try(:granted?)
目前尚不清楚许可的存在是否意味着授予许可,或者也许该权限是否具有诸如granted?
之类的属性。同样,您的问题是模棱两可的。