Topic: How to give an ID to an input when using select helpers?

If I'm using a date_select helper how can I tell the helper to add an ID to the select, the name of the element is set (to model[attribute]) of course, but I cant find how to add an ID.

<%= date_select("search", "date", :discard_year => true, :order => [:day, :month]) %>

It does not seem to appear in the Rails API...

<select name="search[date][day]">
  <option value="1">1</option>
  <option value="31">31</option>

<select name="search[date][month]">
  <option value="1">January</option>
  <option value="12">December</option>

Re: How to give an ID to an input when using select helpers?

Sorry this isn't very helpful, but I don't think it's possible. Date selectors in rails don't seem to offer a whole lot in the customization department. You can try wrapping each select in a span with an id and try to get to the select that way (depending upon how you were going to use the "id" attribute).

<span id="day_field">
  <%= select_day(, :prefix => 'search[date]') %>
<span id="month_field">
  <%= select_month(, :prefix => 'search[date]') %>

The only other option I see is building your own date selector.

Okay, I take that back. It is possible but with an EXTREMELY ugly hack.

<%= select_day(, :prefix => 'search[date][day]" id="day_field', :discard_type => true) %>
<%= select_month(, :prefix => 'search[date][month]" id="month_field', :discard_type => true) %>

That is a really bad hack and I don't even recommend it (it may very well break in a future rails version). I just wanted to show it is possible. wink

Last edited by ryanb (2006-07-05 14:50:34)

Railscasts - Free Ruby on Rails Screencasts

Re: How to give an ID to an input when using select helpers?

Thanks for the reply, I had a feeling it may not be possible since its not in the API docs.
I need the ID to be on the actual input tag because I need to change its value using javascript.
I could either try the hack or just make some partials with the helper generated html in them, adding the IDs of course...

I guess if I have time the best thing to do would be to upgrade the helpers to include this functionality and submit it back to the Rails project for inclusion.

AFM smile

Re: How to give an ID to an input when using select helpers?

Most ActionView helpers include an id of the form "#{object}_#{method}".  In situations like radio buttons where there needs to be slightly more information in the id there's another underscore and then the value of the radio button.  Does anyone know why these select fields don't default to an id of the same format?

Yeah, this should definitely be made into a patch.  The Rails team prefers that you just submit a test to test this functionality and let them add the actual code - anybody up to the challenge?