Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an optgroup Using simple_form gem on Rails 3.2

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.

like image 844
Tim Knight Avatar asked Apr 09 '12 22:04

Tim Knight


2 Answers

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.

like image 188
Tim Knight Avatar answered Oct 23 '22 08:10

Tim Knight


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

like image 31
Tikiboy Avatar answered Oct 23 '22 07:10

Tikiboy