Rails - 集合集的单选按钮

问题描述:

我有以下输出选择框:

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

代替输出单选按钮的 rails 方法是什么?

What is the rails method to instead output Radio Buttons?

对于单选按钮,您必须自己迭代并输出每个单选按钮及其标签.其实很简单.

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 %>

或者在 haml 中,以防它比 erb 更受欢迎:

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