Forum: Ruby on Rails check_box:how to update 2 checkboxes dependent on each other

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.
Michel D. (Guest)
on 2006-06-07 16:44
I've got the following problem,

I've created two check_boxes in my view. The user has to select one
check_box,
but isn't allowed to seth both of them together on true. Now i am
looking for functionality that can help me doing the folling:

Scenario 1
 CheckBox1 = True, The user sets CheckBox2 from False to True, now
 CheckBox1 should automatically become False.

Scenario 2
 CheckBox2 = True, The user sets CheckBox1 from False to True, now
 CheckBox2 should automatically become False.

Is there somebody, who knows how to build a function for that?


The select_boxes in my view look like this:

<%= check_box 'account', 'agriculture' %>
<%= check_box 'account', 'vat_duty' %>

-> agriculture and var_duty are both existing fields in my model.

Thnx Michel Dogger
Daniel -. (Guest)
on 2006-06-07 18:01
(Received via mailing list)
Try radio buttons.  These only allow one selection at a time
Brian H. (Guest)
on 2006-06-07 18:11
(Received via mailing list)
Almost sounds like radio buttons, or am I not understanding your
question?

True (  )    False  (  )
Brian H. (Guest)
on 2006-06-07 18:17
(Received via mailing list)
Nevermind.... I get it... they're two separate options.

You're describing something typically done with Javascript. onclick
functions on each button would modify the other button.

THere are things like observe_field and other helpers in Rails that
are available for this, but it might be pretty easy to do it yourself.
Chris K. (Guest)
on 2006-06-07 18:44
Still, it sounds like you should use radio buttons. It'll be more
accessible that way--this is what radio buttons were designed for.

Then, take care of the different variables in your model at the
application level. Perhaps with a before_save in your model?
Michel D. (Guest)
on 2006-06-08 13:32
Thnx guys,

I've switched over to use radio buttons, and
that, for the moment, works fine for me.

micheldogger
This topic is locked and can not be replied to.