开发者

Set HTML id attribute in Rails form select box

How do I set the id attribute in my drop down boxes?

Here is the part of my form in question:

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

The :id => "quantity_field" method works for text fields, but not for the select fields. Viewing the HTML source I am getting an id of "estimate_items_attributes_0_material" for the material text box.

This is a strange inconsistency. Any help will be greatly appreciated.


There is a parameter between the possible choices and the html options. So you have to do this :

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

You can find the doc here : http://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-select

And this one can also be helpful : http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-select

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜