在我的Rails应用程序中,我有一个名为customer creation的模块,customers表看起来像
create_table "customers", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "name"
t.string "address"
t.string "affiliate_id"
t.string "category"
t.string "domain"
t.string "phone"
t.string "contact"
t.string "email", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.text "comments", limit: 65535
end
新客户的表格如下
<%= form_for @customer do |f| %>
<% if @customer.errors.any? %>
<div id="error_explanation">
<h2>
<%= "#{pluralize(@customer.errors.count, "error")} prohibited this customer from being saved:" %>
</h2>
<ul>
<% @customer.errors.full_messages.each do |msg| %>
<li>
<%= msg %>
</li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, class:"form-control" %>
</div>
<div class="form-group">
<%= f.label :address %>
<%= f.text_field :address, class:"form-control" %>
</div>
<div class="form-group">
<%= f.label :affiliate_id %>
<%= f.text_field :affiliate_id, class:"form-control" %>
</div>
<div class="form-group">
<label> Category </label>
<%= f.select :category, [["Alexa Sills" ,"Alexa Sills" ], ["Appliances","Appliances"],["Vehicles","Vehicles"],["Video Games","Video Games"],["Wine","Wine"],], {id: 'category', prompt: 'All Departments'}, class:"form-control" %>
</div>
<div class="form-group">
<%= f.label :domain %>
<%= f.text_field :domain, class:"form-control" %>
</div>
<div class="form-group">
<%= f.label :phone %>
<%= f.text_field :phone, class:"form-control" %>
</div>
<div class="form-group">
<%= f.label :contact %>
<%= f.text_field :contact, class:"form-control" %>
</div>
<div class="form-group">
<%= f.label :email %>
<%= f.text_field :email, class:"form-control" %>
</div>
<div class="form-group">
<%= f.label "Additional Details" %>
<%= f.text_area :comments, rows: 10, class:"form-control" %>
</div>
<div class="form-group">
<% f.fields_for :additional_fields do | ing | %>
<%= ing.text_field :key, :size => 50 %>
<%= ing.text_field :value, :size => 50 %>
<% end %>
</div>
<div class="actions">
<div class="row">
<div class="col-md-5">
<!--%= f.submit '+', :name => "add_additional_fields" %-->
<span><%= f.submit 'Save', class: "btn btn-success" %></span>
</div>
</div>
</div>
<% end %>
如何在创建新客户时选择多个类别,以便我可以向该客户显示相关产品。 请建议最好的方法。 在此先感谢!
答案 0 :(得分:0)
您可以使用 select2.js 来提供多个选项,您可以查看here
答案 1 :(得分:0)
在类别下拉列表中使用multiple => true
:
<div class="form-group">
<label> Category </label>
<%= f.select :category, [["Alexa Sills" ,"Alexa Sills" ],["Appliances","Appliances"],["Vehicles","Vehicles"],["Video Games","Video Games"],["Wine","Wine"],], {id: 'category', prompt: 'All Departments'},{ :multiple => true}, class:"form-control" %>
</div>
答案 2 :(得分:0)
您需要允许此阵列
1)使用:category[]
而非仅:category
2)在你的控制器的私有方法中,还要在类别参数后添加括号“[]”。
你需要在你的html表单中使用multiple = true,如另一个人所提及的那样:)