beginners question

hi group,

I am making a little rails app to experiment with rails. It consists
of one model, item, that represents an item on a todo list.
A todo item has a description, a state (finished or not) and a due
After changing the scaffold screens a bit, I wanted to be able to
have no due date. I tried to do this by making a radio button; if 'no
date' is selected, the date selection field should be disabled.

I have something working now, but I have this feeling that it is not
exactly how it should be done in rails. Appended to this mail is my
app/views/item/edit.html.erb. Comments/suggestions welcome!
Especially the javascript part is bothering me: should it really be
included in the html.erb file and at that point? Another thing is the
radio_button_tags with the onclick. This is not good, is it?

thanks in advance, Ruud

Hey ruud- You could attempt this using ajax in an effort to keep
things neater.

If you add <%= javascript_include_tag :defaults %> to your views then
rails will also load up the prototype library.

This gives access to loads of helpful js and ajax functions. - read up on <%= observe_field(field_id,
options = {}) %>

Using this method in the view you could observe the radio button and
when it's pressed, call an action in the controller to change the
datetime select.

The JS function you've given seems fine to though and for such a
simple operation, I'd probably just go with that.
By including <%= javascript_include_tag :defaults %> you'll also have
access to the application.js file in your public/javascripts dir.
You can pop the js in there to keep it out of the view.

Hope that helps?

Hi Gavin,

to be honest, I started with an ajax and a js-partial, but I could not
get the js-partial working. So I thought ajax was maybe not the way to
solve this. I wil try your link first. Maybe I'll find there an
explanation what I should have done.

Thanks for your pointer,


hi Gavin,

I inserted two observe_field calls, essentially replacing the onclick
values on the radio button tags. But it turns out that only the first
time a radio button is clicked, the :function of the observer is
called. So if I switch back, nothing happens. It does if I reload the
Do I miss something in the documentation?

regards, Ruud

[ options hash delete from input tags]
    <%= observe_field( :date_none, :function => 'date_clicked(true)')
    <%= observe_field( :date_with, :function => 'date_clicked(false)')

Hi Ruud

Hmm - I've never used observe_field to call a js function, usually
just for rails-ajax calls.

If I were calling a js function like so, I'd normally just
add :onclick => 'date_clicked(true)' to the radio buttons.

There should be a way to call the js function using observe_field
though so let's try and find it?

Why not try simplifying the function call first, with something
like :function => "alert('this works')" just to check if this is where
the problem is?

what do you have in the date_clicked() function now?

Hi Gavin,

I had the same idea:

function datum_geklikt (geenDatum)
    var doc = window.content.document;
    doc.getElementById( 'dagid').disabled = geenDatum;
    doc.getElementById( 'maandid').disabled = geenDatum;
    doc.getElementById( 'jaarid').disabled = geenDatum;

That's why I can say for sure the function is only called on the first
click. Only directly after the first click, I get the alert (note the
variable names are different; I translated the first posts, this one
is untranslated). It is not the button, because after a reload, I get
the alert, no matter if the first button was checked first, or the
second button.
Of course the disabling/enabling of the date select is only done once
too; although the error console shows nothing.

I hope you can shine a light on this one; I don't know what I should
start with...

regards, Ruud

If observe_field isn't going to call more than once, but you could try
observe_form instead?

<%= observe_form :new_item, :function => 'datum_geklikt()' %>

I know it's not ideal but instead of passing arguments true and false
to the function, have the function determine the current value of the
radios (or one radio) and change the values appropriately.

These are just ideas, I'm afraid I'm not too experienced with ajax and

Maybe one of the more experienced users can lend a hand if this
doesn't work?

Hi Gavin,

I tried your suggestion; this time the javascript function isn't
called at all.

             <% form_tag({}, {:id => 'radioform'}) do %>
                <%= radio_button_tag( 'datum', 'geen', @geendatum )
                <%= f.label( 'geen datum' ) %><br/>
                <%= radio_button_tag( 'datum', 'met', !@geendatum) %>
                <%= f.label( 'met datum' ) %>
             <% end %>
            <%= observe_form( :radioform, :function => 'datum_geklikt
(true)') %>

It appears that the observe_form results in a error on the error

Error: $(form) is null
Source file: ..../javascripts/prototype.js .....

I have a feeling I am sticking my head into a wasp nest. I am really
interested in understanding what happens, but maybe I should begin
with the regular stuff. I appreciate your help Gavin. If I have time
this week to have a look at it again and I find an answer to this
problem, I will put it in this thread.

thanks, RUud

Ruud - I hope you don't mind but I emailed you some material on the
Prototype and Scriptaculous libraries.

Might be in your junk mail box?