2011-09-02 52 views
3

如何在我的下拉框中設置id屬性?在Rails窗體選擇框中設置HTML id屬性

這裏是我的表格有問題的部分:

<%= f.fields_for :items do |builder| %> 
    <%= builder.label :description %><br /> 
    <%= builder.text_field :description %><br /> 
    <%= builder.label :material %><br /> 
    <%= builder.select :material, @letters.map { |l| [l.material, l.material] }, :id => "material_field" %><br /> 
    <%= builder.label :height %><br /> 
    <%= builder.select :height, @letters.map { |l| [l.height, l.height] }, :id => "height_field" %><br /> 
    <%= builder.label :thickness %><br /> 
    <%= builder.select :thickness, @letters.map { |l| [l.thickness, l.thickness] }, :id => "thickness_field" %><br /> 

    <%= builder.label :quantity %><br /> 
    <%= builder.text_field :quantity, :id => "quantity_field" %> 
    <%= builder.link_to_remove "Remove this item" %> 
<% end %> 

的:ID =>「quantity_field」方法適用於文本字段,而不是爲選擇字段。查看HTML源代碼我得到材料文本框的「estimate_items_attributes_0_material」標識。

這是一個奇怪的不一致。任何幫助將不勝感激。

+1

這是缺點的位置參數,而不是命名參數。 http://en.wikipedia.org/wiki/Connascence_(computer_programming) – Kris

回答

24

在可能的選項和html選項之間有一個參數。所以,你必須這樣做:

<%= builder.select :thickness, @letters.map { |l| [l.thickness, l.thickness] }, {}, :id => "thickness_field" %> 

您可以在這裏找到的文檔:http://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-select

而這其中也可能會有所幫助: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-select

+0

這工作!感謝您的幫助。 –

+0

謝謝......這裏寫着未來=) –