Ruby on Rails表单:如何创建CheckBox表(或列表) [英] Ruby on Rails Forms: how to create a CheckBox Table (or List)

查看:79
本文介绍了Ruby on Rails表单:如何创建CheckBox表(或列表)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Ruby on Rails中创建一组相关复选框的最佳方法是什么?在Turbogears使用的ToscaWidgets库中,您可以执行以下操作:

What's the best way to create a related set of checkboxes in Ruby on Rails? In the ToscaWidgets library used by Turbogears you can do the following:

 twf.CheckBoxTable('arbitrary_numbers', 
         num_cols=5, 
         options=['1','2','3','4','5','6','7','8','9','10']),

这会在5个复选框的两行中生成10个带标签的复选框。我试图在Rails中重复此操作,而不仅仅是创建10个单独的复选框控件。没什么大不了,只是希望有一个干净的方法来做到这一点。

This generates 10 labeled checkboxes in two rows of 5 checkboxes. I'm trying to duplicate this in Rails without just creating 10 separate checkbox controls. No big deal, just hoping for a clean way to do this.

推荐答案

类似这样的事情:

<% 10.times do |i| %>
  <%= label_tag i %>:
  <%= check_box_tag "alternate_numbers[#{i}]" %> <br />
<% end %>

将产生10个复选框,如果您将其放入表单并提交,则可以访问它放在 params [:alternate_numbers] [index] 中,其中number是您的电话号码。您可以将其放入助手中并多次调用。您还可以向助手添加一些参数以自定义输出。

will produce 10 checkboxes and if you will put it into form and submit it, you will have access to it in params[:alternate_numbers][index] where number is your number. You can put it into helper and call many times. You can also add some parameters to helper to customize output.

这篇关于Ruby on Rails表单:如何创建CheckBox表(或列表)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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