8

i'm using the simple_form gem and really like its simplicity. However, trying to set to set up a simple radio button is beyond me as I keep getting the following error

"undefined local variable or method `vegan'"

1.Here what I have so far

 <%= f.collection_radio_buttons :diettype, [[vegan, 'vegan'] ,[vegetarian, 'vegetarian']]%>

2.Heres the code I used before simple_form with an update/patch method, which would update and save a users requirement

 <%= f.label(:diettype, "Vegan") %>
 <%= f.radio_button(:diettype, "Vegan") %>
 <%= f.label(:diettype, "vegetarian") %>
 <%= f.radio_button(:diettype, "vegetarian")%>

3.And here is what I am trying to reproduce

<select>
   <option> vegan </option>
   <option> Vegetarian </option>
</select>

NOTE - vegan and vegetarian are select options that will be stored in the database column of :diettype.

4
  • what is vegetarian and vegan? Commented Feb 18, 2014 at 13:49
  • They are radio buttons for my form, that I'd like the user to select Commented Feb 18, 2014 at 13:53
  • 1
    try: [['vegan','vegan'],['vegetarian', 'vegetarian']]. Or: [[f.vegan,'vegan']] if a database field Commented Feb 18, 2014 at 13:57
  • These are options that will be stored in the database column of :diettype as mentioned above Commented Feb 18, 2014 at 14:00

2 Answers 2

11

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 %> 
Sign up to request clarification or add additional context in comments.

5 Comments

Can you please explain this line of code? <%= f.collection_radio_buttons : diettype, @types, :diettype, :diettype %>
Do you have any values in diettype column of your table? That error would be thrown if your collection is empty, i.e., @types = nil. I used @types as you said that select options that will be stored in the database column of :diettype. So, they should be present in order to display the list.
please see my edit. I want to submit vegan or vegetarian values selected from the select option to :diettype coloum
@samsos ok. so you need static collection with vegan and vegetarian as values. I'll update the answer.
Hey, when trying to use this solution, I get an error stating that I am trying to call a private method select on my model. What am I missing?
8

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'] %>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.