Reputation:
I have two checkboxes that need to be exclusive.
%p Are you a Borrower or Lender?
= f.check_box :borrower, {}, "true", "false"
Borrower
= f.check_box :lender, {}, "true", "false"
Lender
How do you I change this so user can only select either borrower
or lender
option? As currently is, one could check both boxes.
EDIT #2:
So, from what I understand, I need to give the radio_button the same name in order to not confuse the browser and then define a method. Please provide some guidance on how to write such a method.
= f.radio_button(:lender_or_borrower, :lender)
Lender
= f.radio_button(:lender_or_borrower, :borrower)
Borrower
Upvotes: 1
Views: 1026
Reputation: 3847
As you mentioned in your comment, since it semantically only makes sense to have one option selected at a time, you should either use a select or radio_buttons. For example a select could look like this:
f.select(:borrower_or_lender, [['Borrower, borrower'], ['Lender', 'lender']])
See also http://guides.rubyonrails.org/form_helpers.html#select-boxes-for-dealing-with-models
Edit: As for radio buttons: both radio buttons need to have the same method/attribute (the first argument you pass to the helper):
= f.radio_button(:borrower_or_lender, 'borrower')
Borrower
= f.radio_button(:borrower_or_lender, 'lender')
Lender
See also http://guides.rubyonrails.org/form_helpers.html#radio-buttons or http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-radio_button
Upvotes: 2