kamilleski kamilleski - 2 months ago 8
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| %>
<table>
<tr>
<th>Name</th>
<th>Presence</th>
</tr>

<% @all_symptoms.each do |s| %>
<tr>
<td>
<%= s.name %>
</td>
<td>
<%= 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 %>
</td>
</table>
<% end %>

Answer

Have you tried this?

<%= ff.radio_button :presence, true, :value => true, name: "presence_#{s.id}" %>
Comments