views:

292

answers:

1

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.

+2  A: 

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.

klew
Thank you, that gives me a great starting point! My actual issue is not so perfectly linear and sequential, but that shows me the right direction.
Donald Hughes