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.


Hope that helps?

On Feb 26, 12:09 pm, ruud <[email protected]> wrote:
> 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)
>
> <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;}
>
> </script>
>
> <% form_for(@item) do |f| %>
>   <%= f.error_messages %>
> <table>
>     <tr>
>         <td><%= f.label :description %></td>
>         <td><%= f.text_area( :description, { :cols  => 40,
>                                             :rows   => 2}) %></td>
>     </tr>
>     <tr>
>         <td>finished</td>
>         <td><%= check_box_tag( 'finished', @item.finished) %></td>
>     </tr>
>     <tr>
>         <td><%= f.label :priority %></td>
>         <td><%= f.select( :priority, [ 1, 2, 3, 4, 5,]) %></td>
>     </tr>
>     <tr>
>         <td><%= radio_button_tag(  'datum', 'none', @nodate , {
>                                         :name       => 'datum',
>                                         :value      => 'none',
>                                         :onclick    => 'date_clicked
> (true)'
>                                             }) %>
>         <%= f.label( 'no date' ) %></td>
>         <td></td>
>     </tr>
>     <tr>
>         <td><%= radio_button_tag( 'datum', 'with', !...@nodate, {
>                                         :name       => 'datum',
>                                         :value      => 'with',
>                                         :onclick    => 'date_clicked
> (false)'
>                                             }) %>
>             <%= f.label( 'due date' ) %></td>
>         <td>
>     <%= 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' }) %></td>
>       </tr>
>   </table>
>     <%= f.submit "change" %>
> <% end %>
> <%= link_to 'show', @item %> |
> <%= link_to 'back', items_path %>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to