在作为关联品牌的Product模型中,我希望有一个输入字段,我可以在其中选择一个现有品牌或添加一个新品牌。
在我所拥有的表单中:
<%= 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。
此外,对于创建新品牌的部分,我将尝试使用带有tags选项的select2。还有其他建议吗?
发布于 2016-08-26 03:41:43
我想可能是因为你的options_from_collection_for_select(Brand.all, :name, :name, product.brand.name)
我相信对于第二个参数,您需要的是id而不是:name。
参考:http://apidock.com/rails/v4.2.1/ActionView/Helpers/FormOptionsHelper/options_from_collection_for_select
https://stackoverflow.com/questions/39151962
复制相似问题