Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding blank option to rails select tag

Im using jquery chained, and Im trying to get the second drop down list to gray out if the first has a blank option selected. Im assuming I need a blank option in the second list for it to lock out, but I'm not sure how to add a blank option. Here is the select option

<%= select_tag :equipment, options_for_select(Equipment.all.collect
                                            { |e| ["#{e.model} - #{e.serialNum}",e.id, 
                                            :class =>"#{e.handReceipt}"]},
                                            html_options = {:id=>'equipment'}) %>

The first drop down list lets you select the hand receipt type, and with jquery chained, the second list only shows records with the appropriate hand receipt attribute.

How would I add a blank option to the above select?

Edit- Here is what I've tried so far -

<%= select_tag :equipment, 
     options_for_select(  [["--",""],
                          Equipment.all.collect{ |e|
                          ["#{e.model} - #{e.serialNum}",
                          e.id, :class =>"#{e.handReceipt}"]}],
                          html_options = {:id=>'equipment'}) %>

This results in an improper display of the list-

<select id="equipment" name="equipment">
   <option value="">--</option>
   <option value="[&quot;M4 - W432156&quot;, 10, {:class=&gt;&quot;Arms Room&quot;}]">[&quot;PSN-13 - 176985&quot;, 1, {:class=&gt;&quot;Commo&quot;}]</option>
</select>

Instead of showing all the records in the table, it just shows a blank option and the second option.

<%= select_tag :equipment, 
     options_for_select(  :include_blank => true,
                          Equipment.all.collect{ |e|
                          ["#{e.model} - #{e.serialNum}",
                          e.id, :class =>"#{e.handReceipt}"]},
                          html_options = {:id=>'equipment'}) %>

Results in the following error -

C:/Users/Sam/Documents/ruby/btrp/app/views/vehicles/edit.html.erb:19: syntax error, unexpected ',', expecting tASSOC
                          e.id, :class =>"#{e.handReceipt}"]},
like image 528
gr0k Avatar asked May 10 '13 18:05

gr0k


1 Answers

Your parameters at options_for_select is wrong, I think that's the right way:

<%= select_tag :equipment,
      options_for_select(Equipment.all.collect { |e|
                           ["#{e.model} - #{e.serialNum}", e.id,
                            { :class =>"#{e.handReceipt}" }]}), 
      :include_blank => true,
      :id => 'equipment' %>

Read more at:

http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-options_for_select

http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html

like image 141
daniloisr Avatar answered Sep 28 '22 18:09

daniloisr