2017-01-30 119 views
0

在我的Rails應用程序,我有一個模塊調用客戶創造和客戶表看起來像選擇多個類別的形式

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

您需要允許該陣列 1)您在您的控制器的私有方法與:category[]而不是僅僅:category

2次)也添加分類參數後面的括號「[]」。

而你需要在你的html表單中有multiple = true作爲被其他人提及的:)

0

您可以使用select2.js這給多個選項,您可以檢查它here

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>