# Beginners question

**URL:** <https://www.ruby-forum.com/t/beginners-question/161433>\
**Category:** Rails\
**Created:** [February 26, 2009, 1:10pm UTC](https://www.ruby-forum.com/t/beginners-question/161433 "2009-02-26T13:10:49Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ruud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@ruud](https://www.ruby-forum.com/u/ruud)\
**Post date:** [February 26, 2009, 1:10pm UTC](https://www.ruby-forum.com/t/beginners-question/161433/1 "2009-02-26T13:10:49Z")

</div>

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  
date.  
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)

# Editing item

\<% form\_for(@item) do |f| %\>  
\<%= f.error\_messages %\>

| \<%= f.label :description %\> | \<%= f.text\_area( :description, { :cols =\> 40, :rows =\> 2}) %\> |
| finished | \<%= check\_box\_tag( 'finished', @item.finished) %\> |
| \<%= f.label :priority %\> | \<%= f.select( :priority, [1, 2, 3, 4, 5,]) %\> |
| \<%= radio\_button\_tag( 'datum', 'none', @nodate , { :name =\> 'datum', :value =\> 'none', :onclick =\> 'date\_clicked (true)' }) %\> \<%= f.label( 'no date' ) %\> | |
| \<%= radio\_button\_tag( 'datum', 'with', !@nodate, { :name =\> 'datum', :value =\> 'with', :onclick =\> 'date\_clicked (false)' }) %\> \<%= f.label( 'due date' ) %\> | \<%= select\_day( @day, { :disabled =\> @nodate }, { :id =\> 'did', :name =\> 'dname' }) %\> \<%= select\_month( @month, { :disabled =\> @nodate }, { :id =\> 'mid', :name =\> 'mname' }) %\> \<%= select\_year( @year, { :disabled =\> @nodate , :start\_year =\> 2009 }, { :id =\> 'jid', :name =\> 'jname' }) %\> |

 \<%= f.submit "change" %\> \<% end %\> \<%= link\_to 'show', @item %\> | \<%= link\_to 'back', items\_path %\>

---

<div class="post-metadata">

**Author:** ![ruud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@ruud](https://www.ruby-forum.com/u/ruud)\
**Post date:** [February 26, 2009, 3:34pm UTC](https://www.ruby-forum.com/t/beginners-question/161433/2 "2009-02-26T15:34:15Z")

</div>

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.  
[http://api.rubyonrails.org/](http://api.rubyonrails.org/) - 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?

---

<div class="post-metadata">

**Author:** ![ruud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@ruud](https://www.ruby-forum.com/u/ruud)\
**Post date:** [February 26, 2009, 4:30pm UTC](https://www.ruby-forum.com/t/beginners-question/161433/3 "2009-02-26T16:30:16Z")

</div>

On 26 feb, 15:33, Gavin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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,

Ruud

---

<div class="post-metadata">

**Author:** ![ruud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@ruud](https://www.ruby-forum.com/u/ruud)\
**Post date:** [February 27, 2009, 6:52pm UTC](https://www.ruby-forum.com/t/beginners-question/161433/4 "2009-02-27T18:52:01Z")

</div>

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  
page.  
Do I miss something in the documentation?

regards, Ruud

[options hash delete from input tags]  
[inserted]  
\<%= observe\_field( :date\_none, :function =\> ‘date\_clicked(true)’)  
%\>  
\<%= observe\_field( :date\_with, :function =\> ‘date\_clicked(false)’)  
%\>

---

<div class="post-metadata">

**Author:** ![ruud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@ruud](https://www.ruby-forum.com/u/ruud)\
**Post date:** [February 27, 2009, 7:24pm UTC](https://www.ruby-forum.com/t/beginners-question/161433/5 "2009-02-27T19:24:35Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ruud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@ruud](https://www.ruby-forum.com/u/ruud)\
**Post date:** [February 28, 2009, 2:06am UTC](https://www.ruby-forum.com/t/beginners-question/161433/6 "2009-02-28T02:06:18Z")

</div>

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  
rails.

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

---

<div class="post-metadata">

**Author:** ![ruud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@ruud](https://www.ruby-forum.com/u/ruud)\
**Post date:** [February 27, 2009, 10:11pm UTC](https://www.ruby-forum.com/t/beginners-question/161433/7 "2009-02-27T22:11:18Z")

</div>

On Feb 27, 7:23 pm, Gavin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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;  
alert(‘jep’);  
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

---

<div class="post-metadata">

**Author:** ![ruud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@ruud](https://www.ruby-forum.com/u/ruud)\
**Post date:** [March 1, 2009, 12:18pm UTC](https://www.ruby-forum.com/t/beginners-question/161433/8 "2009-03-01T12:18:10Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ruud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@ruud](https://www.ruby-forum.com/u/ruud)\
**Post date:** [February 28, 2009, 10:19pm UTC](https://www.ruby-forum.com/t/beginners-question/161433/9 "2009-02-28T22:19:47Z")

</div>

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’ ) %\>  
  
\<%= 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  
console:

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
