# Date\_select and the bad id

**URL:** <https://www.ruby-forum.com/t/date-select-and-the-bad-id/118536>\
**Category:** Rails\
**Created:** [October 28, 2007, 10:49am UTC](https://www.ruby-forum.com/t/date-select-and-the-bad-id/118536 "2007-10-28T10:49:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![stratboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@stratboy](https://www.ruby-forum.com/u/stratboy)\
**Post date:** [October 28, 2007, 10:49am UTC](https://www.ruby-forum.com/t/date-select-and-the-bad-id/118536/1 "2007-10-28T10:49:02Z")

</div>

Hi!

I’m using date\_select.  
I want to write a js that change the day field based on the month field  
(if I select october, than the day field has 31 days, if I select  
september, it changes to 30, and so on…)

This is the field generated by dat\_select:

I need to target the id with javascript, so I need to abstract it too. I  
would really like to choose the id by myself… I don’t need  
“entry\_entry\_date\_3i”. I need “year”, “month” and “day” (or something  
similar, not tied to the model), so I can use the js in all my projects.

Is there someone that can tell me how to force it?

---

<div class="post-metadata">

**Author:** ![stratboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@stratboy](https://www.ruby-forum.com/u/stratboy)\
**Post date:** [October 28, 2007, 9:17pm UTC](https://www.ruby-forum.com/t/date-select-and-the-bad-id/118536/2 "2007-10-28T21:17:11Z")

</div>

On Oct 28, 5:49 am, Luca R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I need to target the id with javascript, so I need to abstract it too. I  
> would really like to choose the id by myself… I don’t need  
> “entry\_entry\_date\_3i”. I need “year”, “month” and “day” (or something  
> similar, not tied to the model), so I can use the js in all my projects.
> 
> ## Is there someone that can tell me how to force it?
> 
> Posted viahttp://www.ruby-forum.com/.

The problem with changing the id, or even adding a class or something  
is that the date\_select helper generates multiple select elements  
(e.g. year, month, day). Have you considered writing some javascript  
that can create the appropriate id? The id is created as a  
concatenation of the model, the field, and the subtype of date/times,  
and the i character, where subtype values are 1 = year, 2 = month, 3 =  
day, and so on for others. If you only cared about having the right  
number of days for a given month (which is deterministic, but don’t  
forget about leap years!), you would only need to know the value of  
model and field, but that’s not really “generic”.

So a bigger question is, do you really want the default rails date  
selectors??? If you really, really do, then you might lobby the  
author of this patch  
[http://dev.rubyonrails.org/attachment/ticket/9015/date\_helper.rb](http://dev.rubyonrails.org/attachment/ticket/9015/date_helper.rb)  
to create a patch – it seems to address exactly what you are looking  
for (see line 66 of the code compared to the commented out line before  
it). In short, the date\_select fields don’t appear to work like  
others … yet.

Have you considered a more user-friendly approach, like using one of  
several calendar helper plugins, such as  
[http://code.google.com/p/calendardateselect/](http://code.google.com/p/calendardateselect/)  
or [http://wiki.rubyonrails.org/rails/pages/Calendar+Helper+Plugin](http://wiki.rubyonrails.org/rails/pages/Calendar+Helper+Plugin)? In  
addition to ensuring a valid date, they give the user a much nicer  
interface, in my opinion.

Tom

---

<div class="post-metadata">

**Author:** ![stratboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@stratboy](https://www.ruby-forum.com/u/stratboy)\
**Post date:** [October 29, 2007, 9:52am UTC](https://www.ruby-forum.com/t/date-select-and-the-bad-id/118536/3 "2007-10-29T09:52:23Z")

</div>

Ok, thank you very much, really deep explanation. I’ll consider all the  
suggestions. If I’ll choose to write the js anyway, eventually I’ll post  
it here for whoever need it.

thank you very much,

Luca

tharrison wrote:

> On Oct 28, 5:49 am, Luca R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > I need to target the id with javascript, so I need to abstract it too. I  
> > would really like to choose the id by myself… I don’t need  
> > “entry\_entry\_date\_3i”. I need “year”, “month” and “day” (or something  
> > similar, not tied to the model), so I can use the js in all my projects.
> > 
> > ## Is there someone that can tell me how to force it?
> > 
> > Posted viahttp://www.ruby-forum.com/.
> 
> The problem with changing the id, or even adding a class or something  
> is that the date\_select helper generates multiple select elements  
> (e.g. year, month, day). Have you considered writing some javascript  
> that can create the appropriate id? The id is created as a  
> concatenation of the model, the field, and the subtype of date/times,  
> and the i character, where subtype values are 1 = year, 2 = month, 3 =  
> day, and so on for others. If you only cared about having the right  
> number of days for a given month (which is deterministic, but don’t  
> forget about leap years!), you would only need to know the value of  
> model and field, but that’s not really “generic”.
> 
> So a bigger question is, do you really want the default rails date  
> selectors??? If you really, really do, then you might lobby the  
> author of this patch  
> [http://dev.rubyonrails.org/attachment/ticket/9015/date\_helper.rb](http://dev.rubyonrails.org/attachment/ticket/9015/date_helper.rb)  
> to create a patch – it seems to address exactly what you are looking  
> for (see line 66 of the code compared to the commented out line before  
> it). In short, the date\_select fields don’t appear to work like  
> others … yet.
> 
> Have you considered a more user-friendly approach, like using one of  
> several calendar helper plugins, such as  
> [Google Code Archive - Long-term storage for Google Code Project Hosting.](http://code.google.com/p/calendardateselect/)  
> or [http://wiki.rubyonrails.org/rails/pages/Calendar+Helper+Plugin](http://wiki.rubyonrails.org/rails/pages/Calendar+Helper+Plugin)? In  
> addition to ensuring a valid date, they give the user a much nicer  
> interface, in my opinion.
> 
> Tom
