我在Rails 5的丰富的连接表中苦苦挣扎,需要帮助才能步入正轨。我正在编写的应用程序将帮助我跟踪公司的哪些供应商携带哪些品牌的产品。因为我还需要跟踪每个供应商是否对其销售的每个品牌都授权或未授权,以及他们是否携带这些品牌的库存,所以我认为最好的方法是使用联接表并在其中存储属性。换句话说:
Suppliers <---> Lines <---> Brands
除了Supplier
和Brand
的外键引用之外,Line
记录还具有两个布尔属性:.is_authorized
和.carries_stock
。
我的模特:
/models/supplier.rb
class Supplier < ApplicationRecord
has_many :lines, :dependent => :destroy
has_many :brands, :through => :lines
accepts_nested_attributes_for :lines
end
/models/brand.rb
class Brand < ApplicationRecord
has_many :lines, :dependent => :destroy
has_many :suppliers, :through => :lines
end
/models/line.rb
class Line < ApplicationRecord
belongs_to :supplier
belongs_to :brand
validates_presence_of :supplier
validates_presence_of :brand
end
我已经能够设置控制器和供应商编辑表单,以允许在Lines
表中创建记录,但是不知道如何允许用户编辑.is_authorized
和{{ 1}}属性。通过添加以下代码段,我已经能够使创建/编辑供应商表单起作用:
/views/suppliers/_form.html.erb
.carries_stock
The form looks like this now,但不允许我编辑丰富属性 <h4>Brands</h4>
<%= form.collection_check_boxes(:brand_ids, Brand.all, :id, :name) do |b| %>
<%= b.label class:"label-checkbox" do%>
<%= b.check_box + b.text%>
<%end%>
<br />
<% end %>
和.is_authorized
。我想要表格to look something more like this。我从这里去哪里?
谢谢!