Damjan
Damjan

Reputation: 3

mutually exclusive checkboxes in rails

I have a following problem: I have two attributes that are both boolean but should also be mutually exclusive. Because they are two different attributes I can't implement them as radio buttons but must resort to check_boxes.

How can I make certain it is not possible to check both as true? This is a nested form - so it is probable that there would be more than one set of this attributes (as there could possibly be more than one object).

<table>
   <tr>
    <td>
     <%= f.label :prerequisite, "Check if prerequisite:" %>
     <%= f.check_box :prerequisite %> 
    </td>
    <td>
     <%= f.label :punishment, "Check if punishment:" %>
     <%= f.check_box :punishment %> 
    </td>
   </tr>
</table>

EDIT:

When I add this script to the file it works fine for the first occurance of the table of the form, not for the others:

  <script>$("tr").on("change", ":checkbox", function(){
   if( $(this).is(":checked") ) {
       $(this).parent().parent().find(":checkbox").not($(this)).attr('checked', false);
   }});</script>

Upvotes: 0

Views: 229

Answers (1)

user2033671
user2033671

Reputation:

Just add a third radio for neither

<input type="radio" name="lorem" value="prerequisite">prerequisite<br>
<input type="radio" name="lorem" value="punishment">punishment<br>
<input type="radio" name="lorem" value="neither">neither

Upvotes: 1

Related Questions