Forum: Ruby on Rails beginners question

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
35fee05a52be6486f619d56fa42341ac?d=identicon&s=25 ruud (Guest)
on 2009-02-26 13:10
(Received via mailing list)
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

edit.html.erb (hopefully the tags are preserved in the mail)

<h1>Editing item</h1>
<script type='text/javascript'>
function date_clicked (noDate)
    var doc = window.content.document;
    doc.getElementById( 'dagid').disabled = noDate;
    doc.getElementById( 'mid').disabled = noDate;
    doc.getElementById( 'jid').disabled = noDate;

<% form_for(@item) do |f| %>
  <%= f.error_messages %>
        <td><%= f.label :description %></td>
        <td><%= f.text_area( :description, { :cols  => 40,
                                            :rows   => 2}) %></td>
        <td><%= check_box_tag( 'finished', @item.finished) %></td>
        <td><%= f.label :priority %></td>
        <td><%= :priority, [ 1, 2, 3, 4, 5,]) %></td>
        <td><%= radio_button_tag(  'datum', 'none', @nodate , {
                                        :name       => 'datum',
                                        :value      => 'none',
                                        :onclick    => 'date_clicked
                                            }) %>
        <%= f.label( 'no date' ) %></td>
        <td><%= radio_button_tag( 'datum', 'with', !@nodate, {
                                        :name       => 'datum',
                                        :value      => 'with',
                                        :onclick    => 'date_clicked
                                            }) %>
            <%= f.label( 'due date' ) %></td>
    <%= select_day( @day, { :disabled => @nodate }, { :id => 'did',
                                :name => 'dname' }) %>
    <%= select_month( @month, { :disabled => @nodate }, { :id =>
                                :name => 'mname' }) %>
    <%= select_year( @year, {   :disabled => @nodate ,
                                :start_year => 2009 },
                            {   :id => 'jid',
                                :name => 'jname' }) %></td>
    <%= f.submit "change" %>
<% end %>
<%= link_to 'show', @item %> |
<%= link_to 'back', items_path %>
648a71a35ce6e4a1c5cd63451a2a20df?d=identicon&s=25 Gavin (Guest)
on 2009-02-26 15:34
(Received via mailing list)
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?
35fee05a52be6486f619d56fa42341ac?d=identicon&s=25 ruud (Guest)
on 2009-02-26 16:30
(Received via mailing list)
On 26 feb, 15:33, Gavin <> wrote:

> 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,

35fee05a52be6486f619d56fa42341ac?d=identicon&s=25 ruud (Guest)
on 2009-02-27 18:52
(Received via mailing list)
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)')
648a71a35ce6e4a1c5cd63451a2a20df?d=identicon&s=25 Gavin (Guest)
on 2009-02-27 19:24
(Received via mailing list)
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?
35fee05a52be6486f619d56fa42341ac?d=identicon&s=25 ruud (Guest)
on 2009-02-27 22:11
(Received via mailing list)
On Feb 27, 7:23 pm, Gavin <> wrote:

> 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
648a71a35ce6e4a1c5cd63451a2a20df?d=identicon&s=25 Gavin (Guest)
on 2009-02-28 02:06
(Received via mailing list)
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?
35fee05a52be6486f619d56fa42341ac?d=identicon&s=25 ruud (Guest)
on 2009-02-28 22:19
(Received via mailing list)
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
648a71a35ce6e4a1c5cd63451a2a20df?d=identicon&s=25 Gavin (Guest)
on 2009-03-01 12:18
(Received via mailing list)
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?
This topic is locked and can not be replied to.