簡體   English   中英

Rails集合選擇類別和子類別

[英]Rails collection select of categories and subcategories

我在設法實現以下方法時遇到了麻煩:我想將循環放在select field下面,並且只能選擇子類別。

@categories = Category.where(ancestry: nil)
@subcategories = Category.where("ancestry is NOT NULL and name != ''")

<% @categories.each do |category| %>
    <%= category.name %>
      <% unless category.children.empty? %>
        <% category.children.each do |subcategory| %>
          <%= subcategory.name %>
        <% end %>
     <% end %>
<% end %>

我得到以下子類別:

<%= form.collection_select(:category_id, @ subcategories, :id, :name) %>

但是,如何在選擇字段中按順序放置類別(不可選擇)和子類別(可選擇)呢?

<%= form.grouped_collection_select :category_id, Category.roots.order(:name), :children, :name, :id, :name, {:include_blank => true}, {class: "", :multiple => true } %>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM