Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple sets of radio buttons in a rails view

Is there a way to set up two groups of radio buttons in Rails? I can imagine you could put them into separate forms perhaps but is there a way to create two sets of radio buttons within one form?

like image 607
keybored Avatar asked Feb 10 '11 21:02

keybored


People also ask

Can we select multiple radio buttons?

Only one radio button in a group can be selected at the same time. Note: The radio group must have share the same name (the value of the name attribute) to be treated as a group.

Can you select more than one radio button in HTML?

Radio buttons allow a user to select a single option among multiple options. You can set the Choice Value of each option, for each button, as well as group these buttons by giving them the same Group Name.

How do I select multiple radio buttons in VB net?

To create multiple groups of RadioButtons, you would need to add containers such as Panel or GroupBox and then put RadioButtons inside. After that, if user want to select radio button from each group, the user can able to select one radio button at a single time from one group.


2 Answers

Yes, you can create two different sets by simply using a different radio-button name:

radio_button_tag 'gender', 'male'
radio_button_tag 'gender', 'female'

radio_button_tag 'food', 'none'
radio_button_tag 'food', 'vegetarian'
radio_button_tag 'food', 'vegan'

This will result in params[:gender] being 'male' or 'female' and params[:food] being 'none', 'vegetarian' or 'vegan'. You can do the same thing with the radio_button function.

like image 102
Pan Thomakos Avatar answered Sep 27 '22 22:09

Pan Thomakos


Radio buttons with the same name attributes are grouped. So make sure your Rails code uses the same names for the radio buttons within a group.

According to the documentation the first parameter of the radio_button method is the name, so keep this parameter the same.

like image 41
Veger Avatar answered Sep 27 '22 21:09

Veger