Forum: Ruby on Rails Interesting radio button behavior with "onclick"

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Mike D. (Guest)
on 2007-02-03 23:15
Hello:

I have radio buttons like this:

<% fields_for :goalhist do |g| %>
<table>
<tr>
<td align=left><%= radio_button_tag ('duedate', 0, checked = false,
options = {:onclick => ""}) %>
&nbsp;&nbsp;Start now
</td>
</tr>
<tr id="detailed" style="display: none"><td colspan=2>
</td></tr>
<tr>
<td align=left><%= radio_button_tag ('duedate', 1, checked = false,
options = {:onclick => ""}) %>
&nbsp;&nbsp;Already started!
</td>
</tr>
</table>
<% end %>

So, they behave fine; but, what's interesting is that when I popuulate
the onclick methods, the checking of the radio buttons no longer
responds.  If I leave those methods unpopulated, they perform fine.
Here's my populated code:

<% fields_for :goalhist do |g| %>
<table>
<tr>
<td align=left><%= radio_button_tag ('duedate', 0, checked = false,
options = {:onclick => "new Effect.Fade('duedate'); return false;"}) %>
&nbsp;&nbsp;Start now
</td>
</tr>
<tr id="detailed" style="display: none"><td colspan=2>
</td></tr>
<tr>
<td align=left><%= radio_button_tag ('duedate', 1, checked = false,
options = {:onclick => "new Effect.SlideDown('duedate'); return
false;"}) %>
&nbsp;&nbsp;Already started!
</td>
</tr>
</table>
<% end %>

Any ideas?  Only thing else you might need to know is that the
"fields_for" exists within another "form_for" tag.

Thank you so much in advance!

Mike
Phlip (Guest)
on 2007-02-03 23:35
(Received via mailing list)
Mike D. wrote:

> options = {:onclick => "new Effect.Fade('duedate'); return false;"}) %>

return true ?

--
  Phlip
  http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
Mike D. (Guest)
on 2007-02-03 23:40
Phlip wrote:
> Mike D. wrote:
>
>> options = {:onclick => "new Effect.Fade('duedate'); return false;"}) %>
>
> return true ?
>
> --
>   Phlip
>   http://www.greencheese.us/ZeekLand <-- NOT a blog!!!

Yep, that did it!  Thank you so much!

Any idea as to why?

Mike
_Kevin (Guest)
on 2007-02-03 23:45
(Received via mailing list)
On Feb 3, 4:40 pm, Mike D. <removed_email_address@domain.invalid>
wrote:
>
> Yep, that did it!  Thank you so much!
>
> Any idea as to why?
>
> Mike
>
> --
> Posted viahttp://www.ruby-forum.com/.

I think that lets the click event bubble up to the default click
handlers instead of stopping the chain.  The default handler is what
does the checking/unchecking.

_Kevinj
Phlip (Guest)
on 2007-02-04 04:03
(Received via mailing list)
Mike D. wrote:

> radio_button_tag ('duedate', 1, checked = false,
options = {:onclick => "new Effect.SlideDown('duedate'); return
false;"})

I forgot to point something out about that style.

Ruby functions can't see variables on their command lines written like
checked=

The value goes in, but the name of the variable on the left side of
the assignment does not. Ruby doesn't care if you wrote monkey= or
spider= or nothing there. So prefer nothing; programs should have as
few moving parts as possible!

--
  Phlip
  http://c2.com/cgi/wiki?ZeekLand  <-- NOT a blog!!
Mike D. (Guest)
on 2007-02-04 17:46
Phlip wrote:
> Mike D. wrote:
>
>> radio_button_tag ('duedate', 1, checked = false,
> options = {:onclick => "new Effect.SlideDown('duedate'); return
> false;"})
>
> I forgot to point something out about that style.
>
> Ruby functions can't see variables on their command lines written like
> checked=
>
> The value goes in, but the name of the variable on the left side of
> the assignment does not. Ruby doesn't care if you wrote monkey= or
> spider= or nothing there. So prefer nothing; programs should have as
> few moving parts as possible!
>
> --
>   Phlip
>   http://c2.com/cgi/wiki?ZeekLand  <-- NOT a blog!!

Phlip:

I was wondering about that - so thanks for clearing it up for me. Looks
like the left-hand of the assignment is only helpful for the programmer
and not for the framework.  So whatever belongs in that position when
the appropriate method is called is what rails is expecting.

So that begets the question of how to handle empties or can you re-order
if you use name assignments.  Based on what you said here, seems like
not? Is that correct?
Phlip (Guest)
on 2007-02-04 18:10
(Received via mailing list)
Mike D. wrote:


> I was wondering about that - so thanks for clearing it up for me. Looks
> like the left-hand of the assignment is only helpful for the programmer
> and not for the framework.  So whatever belongs in that position when
> the appropriate method is called is what rails is expecting.
>
> So that begets the question of how to handle empties or can you re-order
> if you use name assignments.  Based on what you said here, seems like
> not? Is that correct?

Inferior languages use two systems to name arguments - Hashes/Maps, and
named parameters. So such a language, with a Ruby-like syntax, would
permit
this:

foo( :arg1 = 'bar', { :arg2 => 'baz' } )

That requires a foo with this signature:

  foo(arg1, aMap)

Ruby splits the difference by not using two systems there, only one.
Using
only one system, instead of two, is always a Good Thing - it's an
example of
the DRY principle, Don't Repeat Yourself.

All Ruby arguments are positional, and arg1 may not follow aMap. But
Ruby
allows you to leave out the map notation {}. So this...

 foo('bar', :arg2 => 'baz')

...is the only way to call foo(arg1, aMap). arg1 is not optional, but
arg2
is.

This technique returns incredible control to the function author. She or
he
can detect which argument is a Hash and proceed accordingly. A function
could take only a map, for example, and the caller can pass named
arguments
in any order so long as their names match.

Now read up on Hash#merge and Hash#update. You handle an empty by
checking
if aMap[:arg2] is nil, or simply by merging a default Hash into aMap
before
pulling its arguments. Or updating - I forget which.

--
  Phlip
  http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
Phlip (Guest)
on 2007-02-04 18:19
(Received via mailing list)
>> Mike D. wrote:
>>
>>> radio_button_tag ('duedate', 1, checked = false,
>> options = {:onclick => "new Effect.SlideDown('duedate'); return
>> false;"})

Another answer. Rails authors who cram huge long statements like that
together should be lined up and smacked. options= there is a
best-practice
when you simply use it as an argument, to break up long lines:

options = {:onclick => "new Effect.SlideDown('duedate'); return
false;"})

radio_button_tag ('duedate', 1, false, options)

Now you can actually see where expressions begin and end!

The other way around, options= inside the method call, is also bad
because I
might think options is used somewhere below, and it isn't.

--
  Phlip
  http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
This topic is locked and can not be replied to.