kamilleski kamilleski - 1 year ago 119
Ruby Question

Unique radio buttons when iterating in rails

I'm creating an application in which I have a list of Symptoms (@all_symptoms) over which I am iterating and I want to have radio buttons for each one indicating whether its presence is true or false. The problem I keep running up against no matter what I try is that the radio buttons are all named the same, so I can only select true/false once on the entire form.

I tried changing the label to:

<%= ff.radio_button "presence_#{s.id}", true %>

but there is an error because there is (obviously) no method called "presence_X" for my symptoms. (For clarity: "presence" is an attribute for a Symptom, and it accepts a Boolean value.)

The nested attribute part of the form is because this is part of an Appointment which accepts attributes for Symptoms.

<%= f.fields_for :symptoms do |ff| %>

<% @all_symptoms.each do |s| %>
<%= s.name %>
<%= ff.label :presence, "True", :value => true %>
<%= ff.radio_button :presence, true, :value => true %>
<%= ff.label :presence, "False", :value => false %>
<%= ff.radio_button :presence, false, :value => false %>
<% end %>

Answer Source

Have you tried this?

<%= ff.radio_button :presence, true, :value => true, name: "presence_#{s.id}" %>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download