Check_box:how to update 2 checkboxes dependent on each other


#1

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


#2

Try radio buttons. These only allow one selection at a time


#3

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.


#4

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?


#5

Almost sounds like radio buttons, or am I not understanding your
question?

True ( ) False ( )


#6

Thnx guys,

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

micheldogger