Search code examples
ruby-on-rails-3optgroup

awesome_nested_set with optgroup labels


I am using the awesome_nested_set gem located here https://github.com/collectiveidea/awesome_nested_set. I would like to make the parent categories optgroup labels but am at a loss on how to make that happen or if it is even possible. Is there an example somewhere of this behavior?


Solution

  • This is certainly not the most efficient way to do this, but it met the requirement of the task.

      <select name="category_id" data-placeholder="Select a Category" class="chzn-select">
         <option value=""></option>
         <% @cats.each do |cat| %>
         <optgroup label="<%= cat.name %>">
            <% cat.children.each do |child| %>
            <option value="<%= child.id %>"><%= child.name %></option>
            <% end %>
         </optgroup>
         <% end %>
      </select>