Simple form radio button
In your controller action
, add this line
def actionname
@types = ModelName.select(:diettype).distinct
..
end
where,
actionname
is the action which is rendering your view.
ModelName
is the name of your model which has diettype
field in it.
In your view
, replace
<%= f.collection_radio_buttons :diettype, [[vegan, 'vegan'] ,[vegetarian, 'vegetarian']]%>
with
<%= f.collection_radio_buttons :diettype, @types, :diettype, :diettype %>
EDIT:
<%= f.collection_radio_buttons :diettype, @types, :diettype, :diettype %>
The above line means:
Create a collection of radio buttons where,
1st :diettype
: variable would be set when you select a radio button
@types
: this collection being passed
2nd :diettype
: the value that is being selected
3rd :diettype
: the display text beside the button
EDIT2
As you specified that you need a static collection
and you are not taking any values from database :
Simply add the following line in view, no need to change the controller :
<%= f.collection_radio_buttons :name, [['vegan', 'vegan'] ,['vegetarian', 'vegetarian']],:first, :last %>
This is the simplest solution for adding radio buttons with Simple Form that works great for me.
<%= f.input :diettype, as: :radio_buttons, collection: ['Vegan', 'Vegetarian'] %>