2013-05-10 85 views
5

即時通訊使用jquery鏈接,我試圖讓第二個下拉列表變灰,如果第一個選擇了空白選項。我假設我需要在第二個列表中的空白選項鎖定,但我不知道如何添加一個空白選項。這裏是選擇選項添加空白選項導軌選擇標記

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

的第一個下拉列表,可以選擇手工收據類型,並與jQuery鏈,第二個列表僅顯示與適當的手收據屬性記錄。

我該如何爲上面的選擇添加一個空白選項?

編輯 - 這是我到目前爲止已經試過 -

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

這導致的列表 -

<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> 

而不是顯示錶中的所有記錄的顯示不正確,它只是顯示一個空白選項和第二個選項。

<%= 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'}) %> 

結果在下面的錯誤 -

C:/Users/Sam/Documents/ruby/btrp/app/views/vehicles/edit.html.erb:19: syntax error, unexpected ',', expecting tASSOC 
          e.id, :class =>"#{e.handReceipt}"]}, 
+0

請注意,您的'html_options'是錯誤的。您需要使用散列格式,所以':html_options => {:id =>'equipment'}'是正確的 – daniloisr 2013-05-10 19:16:44

+0

注意和固定。它之前正確設置它並沒有拋出錯誤。謝謝你的收穫。 – 2013-05-10 19:23:37

回答

13

你在options_for_select參數是錯誤的,我認爲這是正確的做法:

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

更多詳情:

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

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

+0

感謝您的提示。有關如何在選擇中獲得空白選項的任何想法? – 2013-05-10 20:06:26

+0

對不起,我忘了=) – daniloisr 2013-05-10 20:11:42

+1

那該死的。哈哈我一直都在努力讓這個工作整個上午。我認爲include_blank將在options_for_select下。猜猜我必須花更多的時間來解釋幫助語法。我已經閱讀並重新閱讀了該頁面一千次。謝謝您的幫助! – 2013-05-10 20:20:31

0

您可以像這樣追加空白值。

<%= select_tag "category","<option value=''>Category</option>" + options_from_collection_for_select(@store_categories, "id", "name",params[:category].to_i)%> 
相關問題