Rails - 集合集的单选按钮 [英] Rails - Radio Buttons for collection sets

查看:43
本文介绍了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

这篇关于Rails - 集合集的单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆