Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 783
Corey Quillen Avatar asked Nov 28 '22 09:11

Corey Quillen


1 Answers

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

like image 142
Alexandre Butynski Avatar answered Dec 01 '22 01:12

Alexandre Butynski