Ruby on Rails Forms: how to create a CheckBox Table (or List)

∥☆過路亽.° 提交于 2019-12-23 19:17:04

问题


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']),

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.


回答1:


Something like this:

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

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.



来源:https://stackoverflow.com/questions/2351721/ruby-on-rails-forms-how-to-create-a-checkbox-table-or-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!