2016-08-25 38 views
0

在作爲關聯品牌的產品模型中,我希望有一個輸入字段,可以在其中選擇現有品牌或添加新品牌。Rails表單搜索或創建

在表單中,我有:

<%= f.fields_for :brand do |b| %> 
<div class="form-group"> 
    <%= b.label :name, t('brand.one') %> 
    <%= b.select :name, options_from_collection_for_select(Brand.all, :name, :name, product.brand.name), { include_blank: true}, class: '0select2-find-or-create' %> 
</div> 
<% end %> 

,並在型號: 產品:

belongs_to :brand 
accepts_nested_attributes_for :brand, limit: 1 

品牌:

has_many :products 

但每次我改變的一個品牌產品會改變品牌的(按品牌id)名稱,而不是改變ID。

對於創建新品牌的部分,我將嘗試使用select2tags選項。任何其他建議?

回答