ruby on rails: radio buttons for collection select

2019-02-14 03:22发布

问题:

I have a collection select:

  <%= f.collection_select :role, User::ROLES, :to_s, :humanize %>

What is the radio button for this method?

Thanks

回答1:

There is no such helper in Rails 3. In Rails 4, it is collection_radio_buttons.



回答2:

This way..

<%= f.collection_radio_buttons :role, User::ROLES  %>


回答3:

No documentation found for the form builder, but this should work:

<%= f.collection_radio_buttons :my_attribute, my_hash.map {|k,v| [k,v]}, :first, :last do |b| %>
  <div class='my-class'>
    <%= b.radio_button %>
    <%= b.label %>
  </div>
<% end %>