Sapna Mishra
Sapna Mishra

Reputation: 187

Option Group in select tag using rails

I am getting values of @templates in my controller. right now I have structure like this:

<%= select_tag :template ,options_from_collection_for_select(@templates,"id", "name"),{:prompt => "--Built In Templates--",:class=>"form-control m-b-sm required"}%> 

But now I want to add option group in my select_tag how can I create structure something like:

<%= select_tag :template ,grouped_options_for_select(['Built-In Templates',@templates.collect{|v| [v.name, v.id ] }],@templates),{:prompt => "please select",:class=>"form-control m-b-sm required"}%> 

I want to create array of option group.like

<select>
<optgroup label="Built-In Templates">

<option value="id_default">default</option>
    </optgroup>
    <optgroup label="Custom Templates">
       <option value="id_my template">my template</option>
    </optgroup>
  </select>

Upvotes: 2

Views: 2569

Answers (1)

Sapna Mishra
Sapna Mishra

Reputation: 187

I have added one field called type in template model.Type field has radio button built_in and custom so when I create one template we choose radio button of that type. Now, my dropdown for option select is look like this:

<%= select_tag :template ,grouped_options_for_select([['Built-In templates',  @built_in.collect {|v| [ v.name, v.id ] }],['Custom',  @custom.collect {|v| [ v.name, v.id ] }]]),{:prompt => t("please_select"),:class=>"form-control m-b-sm required"}%> 

Upvotes: 1

Related Questions