开发者

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

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 i开发者_StackOverflown 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜