Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - Radio Buttons for collection sets

I have the following which outputs a select box:

<%= f.label :request_type_id %><br />
<% requestTypes = RequestType.all %>
<%= f.collection_select :request_type_id, requestTypes, :id, :title, :prompt => true %>

What is the rails method to instead output Radio Buttons?

like image 884
AnApprentice Avatar asked Nov 11 '10 05:11

AnApprentice


2 Answers

For radio buttons you have to iterate yourself and output every radio button and its label. It's really easy in fact.

<% RequestType.all.each do |rt| %>
  <%= f.radio_button :request_type_id, rt.id %>
  <%= f.label :request_type_id, rt.title %>
<% end %>

Or in haml in case it's preferred over erb:

- RequestType.all.each do |rt|
    = f.radio_button :request_type_id, rt.id
    = f.label :request_type_id, rt.title
like image 93
Max Chernyak Avatar answered Oct 05 '22 12:10

Max Chernyak


In Rails 4 there is a collection_radio_buttons for this:

<%= f.collection_radio_buttons :request_type_id, RequestType.all, :id, :title %>
like image 25
cweston Avatar answered Oct 05 '22 12:10

cweston