Mixing date_select, Javascript, and AJAX - having trouble wi

Hi all. I’m a relatively new Rails and Javascript user, and I’m having
some problems dealing with a date_select helper. Here’s the basic
situation that I already have working. I have a table and a live search
page where a user can type in partial phrases to match against the
various fields. Most of the fields are text_fields, but there is a
text_field_with_auto_complete and a select as well. I have an
observe_field for each input field and it builds up a search query and
updates the search results. All of these fields are in a
form_remote_for form so I can do all the AJAX goodness. So far so good.

Now we want to add a field that is a datetime. The first problem I see
with the date_select is that it has default values, as opposed to being
blank until filled in like the other fields. I don’t want the search
narrowed by this date field until the user actually sets part of the
date. After some thought I disabled the fields on page load and added a
checkbox to enable the fields, so it visually looks like it’s not part
of the search. Some simple Javascript and enable or disable the
date_select fields as the user checks the box. This is where things
start getting ugly.

  1. Can I specify the name of the form_remote_for somehow? Nothing I
    tried worked right. My current jury-rig is to get document.forms[0],
    but that’s a little brittle - if I add another partial it might move to
    forms[1] or the like. Looking at the page source there’s no name given
    at all.

  2. The names of the date_select fields are crazy for Javascript.
    Specifically they end up being something like
    “text_line[recording_submit_date(1i)]” So I can reference
    document.forms[0].checkboxname.checked, but I can’t reference
    document.forms[0].text_line[recording_submit_date(1i)]. How do I get
    ahold of these fields from Javascript?

  3. I punted on that for now and grabbed them by numeric ID (like
    this: form.childNodes[1].childNodes[21]) That’s even more brittle and
    is going to break the next time I touch this form, but it was a start.
    Now I’m finding that I can’t create an observe_field helper either, and
    I’m guessing that might be due to the crazy names.

I realize that there are probably other plug-ins I can get to handle
date fields differently, but this seems like a simple problem, but it’s
also one of those problems where at four hours ago I started on
something I expected to spend 30 minutes on and I’m still at it. :slight_smile: If
anybody knows a solution that uses the default Rails date_select I’d
love to hear it.