I'm working with rails 5.1.2 and ruby 2.2.6. I'm trying to write an each
loop on a collection and generate a rabio button and a corresponding label for it in my erb
view. So far, I came to this unsatisfying result:
<% @idea.discussions.each do |discussion| %>
<%= radio_button_tag :discussion, discussion.id %>
<%= label_tag "discussion_" + discussion.id.to_s, discussion.title %>
<% end %>
This generates the following HTML for one element of the collection:
<input name="discussion" id="discussion_3" value="3" type="radio">
<label for="discussion_3">Main discussion</label>
What is unsatisfying for me is the first argment of the label_tag
, as I build manually the value of the for
attribute of the label
tag. What is the rails way to do?
Also you can try that:
<% @idea.discussions.each do |discussion| %>
<label for="discussion-<%= discussion.id%>">
<span><%= discussion.title %></span>
<%= radio_button_tag "discussions[discussion_id]", discussion.id, false, { id: "discussion-#{discussion.id}"} %>
</label>
<% end %>