Creating an optgroup Using simple_form gem on Rails 3.2
Asked Answered
B

2

10

There seems to be just a little documentation on the grouped_select feature in simple_form 2.0 at http://simple-form.plataformatec.com.br/#usage/collections. The documentation offers the line:

f.input :country_id, :collection => @continents, :as => :grouped_select, :group_method => :countries

But that doesn't seem to be giving me enough context to make it work. Here's what I have.

I have three models: Inquiry, Ad, and Insertions

Ads has_many Insertions, and Insertions belongs_to Ads Inquiry belongs_to Insertion, and Insertions has_many Inquiries

This dropdown is for the Inquiry view. Using simple_form I can get = f.input :insertion, :collection => @ads to at least output a list of the ad titles in the dropdown. I'd like the ad.title to serve as the optgroup. Then I'd like to have the Ads insertions to serve as the selectable content... so something like:

<select>
  <optgroup label="Ad.Title">
    <option value="Ad.Insertion.id">Ad.Insertion.Title</option>
    <option value="Ad.Insertion.id">Ad.Insertion.Title</option>
    <option value="Ad.Insertion.id">Ad.Insertion.Title</option>
    <option value="Ad.Insertion.id">Ad.Insertion.Title</option>
  </optgroup>
  <optgroup label="Ad.Title">
    <option value="Ad.Insertion.id">Ad.Insertion.Title</option>
    <option value="Ad.Insertion.id">Ad.Insertion.Title</option>
    <option value="Ad.Insertion.id">Ad.Insertion.Title</option>
    <option value="Ad.Insertion.id">Ad.Insertion.Title</option>
  </optgroup>
</select>

Can anyone offer any advise on making this simple_form feature work? I'd really appreciate it!

Please let me know if I can tell you anything else about the app if you have insight on how to execute this property.

Thanks in advance!

UPDATE: I've been able to get something partially working using:

= f.input(:insertion_id, :collection => Ad.order(:name), :as => :grouped_select, :group_method => :insertions)

The problem with this is that there is no way to specify what column is used as the display text from what I can tell. I welcome any input.

Barozzi answered 9/4, 2012 at 22:12 Comment(0)
B
23

After some additional research, looking better at the documentation, and playing with the syntax I've solved what I was looking for.

The standard Rails grouped_collection_select looks like this:

= f.grouped_collection_select(:insertion_id, Ad.order(:name), 
                              :insertions, :name, :id, :title, 
                              include_blank: "Please Choose...")

This can be redone in using simple_form 2.0 with the following syntax:

= f.input(:insertion_id, :collection => Ad.order(:name),
                         :as => :grouped_select,
                         :group_method => :insertions,
                         :group_label_method => :name,
                         :label_method => :title,
                         :include_blank => "Please Choose...")

I hope that helps other people in the future.

Barozzi answered 10/4, 2012 at 2:39 Comment(0)
C
1

An alternative method is:

<%= f.input :product_category do %>
    <%= f.select :product_category, grouped_options_for_select(Product.PRODUCT_CATEGORY), include_blank: true %>
  <% end %>

This works well when you're trying to form an optgroup select without the use of active record modals.

Reference: https://github.com/plataformatec/simple_form#wrapping-rails-form-helpers and http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-option_groups_from_collection_for_select

Chung answered 8/2, 2015 at 11:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.