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?
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/ - 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.
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.
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?
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...
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?
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.