# Select form helper and css attributes

**URL:** <https://www.ruby-forum.com/t/select-form-helper-and-css-attributes/170632>\
**Category:** Rails\
**Created:** [June 19, 2009, 9:30pm UTC](https://www.ruby-forum.com/t/select-form-helper-and-css-attributes/170632 "2009-06-19T21:30:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [June 19, 2009, 9:30pm UTC](https://www.ruby-forum.com/t/select-form-helper-and-css-attributes/170632/1 "2009-06-19T21:30:38Z")

</div>

I am writing a test suite which depends upon begin able to use the html  
id attribute so that the tests are display language independent. The

 tag supports this attribute. In fact it supports, among others, the CLASS, DISABLED, ID, LABEL, SELECTED, STYLE, TITLE, and VALUE attributes. What I cannot seem to discern from the select API is how to set the ID attribute on a specific option.

The API refers to options\_for\_select but I cannot see from that how to  
accomplish what I desire.

The specific case that I am dealing with involves a select box for yes  
or no. The model attribute is a boolean. I desire to have this sort of  
output from the form builder:

**Issue Invoices in this Currency**

```
   <option value="true"
              id="invoicable_true>Yes</option>
   <option value="false" selected="selected"
              id="invoicable_false">No</option>
</select>

```

I can get everything except the id=“invoicable\_true” and  
id=“invoicable\_false” against their specific options.

Is this possible with the existing select form helper or needs be I roll  
my own?

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [June 19, 2009, 9:50pm UTC](https://www.ruby-forum.com/t/select-form-helper-and-css-attributes/170632/2 "2009-06-19T21:50:49Z")

</div>

2009/6/19 James B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Â Â   
> Â Â Â Â Â Â Â Â Â id=“invoicable\_false”\>No  
> Â Â
> 
> I can get everything except the id=“invoicable\_true” and  
> id=“invoicable\_false” against their specific options.
> 
> Is this possible with the existing select form helper or needs be I roll  
> my own?

Assuming I understand the issue correctly then to achieve this I use  
f.select inside a form\_for. This takes an array of values and ids for  
the options. I must admit though that I use integer values for the id  
values so whether this will work in the way you require I am not sure.

Colin

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [June 19, 2009, 10:40pm UTC](https://www.ruby-forum.com/t/select-form-helper-and-css-attributes/170632/3 "2009-06-19T22:40:52Z")

</div>

Colin L. wrote:

> 2009/6/19 James B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > Â Â   
> > Â Â Â Â Â Â Â Â Â id=“invoicable\_false”\>No  
> > Â Â
> > 
> > I can get everything except the id=“invoicable\_true” and  
> > id=“invoicable\_false” against their specific options.
> > 
> > Is this possible with the existing select form helper or needs be I roll  
> > my own?
> 
> Assuming I understand the issue correctly then to achieve this I use  
> f.select inside a form\_for. This takes an array of values and ids for  
> the options. I must admit though that I use integer values for the id  
> values so whether this will work in the way you require I am not sure.
> 
> Colin

I am already using the select helper inside a form\_for construct. Could  
you show me a small example of what you do and what the page source  
generated looks like?

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [June 19, 2009, 11:09pm UTC](https://www.ruby-forum.com/t/select-form-helper-and-css-attributes/170632/4 "2009-06-19T23:09:03Z")

</div>

2009/6/19 James B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> …
> 
> ## I am already using the select helper inside a form\_for construct. Â Could you show me a small example of what you do and what the page source generated looks like?

\<% form\_for(@note) do |f| %\>  
…  
\<%= f.label :location\_id %\>  
  
\<%= f.select :location\_id, Location.get\_names\_and\_ids %\>

provides

Location  
  
Brechfa Blaen-hauliw 1 SN535364

Brechfa Blaen-hauliw 2 SN531362...

Which is not what you are looking for at all ☹

On second thoughts I think you may have to do it yourself.

Sorry

Colin
