Search code examples
ruby-on-rails-3formsformbuilder

Rails FormBuilder - How do use Select with Grouped Options?


I have a Rails form, where I am trying to insert a Select formfield .

My current code runs like this -

 <select id="selectservice"  name="service">


<% @categories.each do |category| %>


      <% @services= category.services %>
      <% @services.each do |service| %>
          <option  value="<%= service.id %>"><%= service.name %></option>
      <% end %>


<% end %>

</select>

Now I want to convert this into a Formbuilder style as the rest of the form is in that style . And also I will be able to insert the variable which is missing here . How do I go about inserting the option tags ?

<%= form_for @appointment  do |f| %>
 <%= f.select :service_id, :name=>"service" %>


##How do I insert the Option tags here ?


<%= end %>

Solution

  • I think I found the answer . This can be achieved by Grouped Options for Select

    <%= f.grouped_collection_select :service_id,
                        @categories, :services, :name,
                        :id, :name , :id=>"selectservice"
                    %>