Ruby on Rails
HowToUseCheckBoxes

To create a check_box and to give it a unique name and a valid value, use something like:

<%= check_box('item_'+i.to_s, 'checked', {}, item.name, '') %>

You can read its value in the action using:

checked = @params['item_'+i.to_s]['checked']

We can use any value for the check_box; it can be just a flag, an id or any other value.
I have used the name of the item because I need it as a key to find the item in the list.

This example uses only a virtual list (not persisted to a database).
You can only verify the correct functioning of the “Delete selection” in the list of the response.
For each request there is a new controller object instantiated and so a new virtual list is created.

Here is an example:

Model

item.rb


class Item
  def initialize(name)
    setName(name)
    setChecked('')    # set to empty  for an unchecked Checkbox
   #setChecked(name)  # set to not empty for a checked Checkbox
  end

  def setChecked(checked)
    @checked = checked
  end

  def checked
    @checked
  end

  def setName(name)
      @name = name
  end

  def name
    @name
  end
end

View

list.rhtml


<h1>Item#list</h1>
<form action='<%= url_for(:action => "deleteSelection") %>' method='POST'>
 <table border="1" cellspacing="2" cellpadding="2" width="30%">
  <tr><th width="5%">Selection</th><th align="left" width="25%">Name</th></tr>
 <% i = 0
    for item in @itemlist do 
      i = i + 1 %>
      <tr>
       <td><%= check_box('item_'+i.to_s, 'checked', {}, item.name, '') %></td>
       <td><%= item.name %></td>
      </tr>
 <% end %>
 </table>
 <br/>
 <input type='submit' value='DELETE SELECTION'/> 
</form>

Controller

item_controller.rb


#require 'item'
class ItemController < ApplicationController
     def initialize
    super
      @itemlist = [Item.new('item1'), Item.new('item2'), Item.new('item3'), Item.new('item4'), Item.new('item5')]
  end

  def list
      @itemlist
  end

  def deleteSelection
    i = 0
      items = @itemlist.clone
      items.each { |item|
        i = i + 1
      if (@params['item_'+i.to_s] != nil) then
        checked = @params['item_'+i.to_s]['checked']
        if (checked != nil && checked.length > 0) then
          if (item.name == checked) then
            @itemlist.delete(item)
          end
        end
      end  
    }
      render_action 'list'
  end
end

Comment:

A simple way to do multi checkboxes without using an iterator to construct an index is to use the brackets in the HTML. Rails automatically does this for you, just use the same variable name for each checkbox.

for product in @products do
  check_box 'checkbox', product.id
end

#then loop through params[:checkbox] to see if the product was selected

Comment:

To set the checkbox to checked by default (if there’s no corresponding boolean in the model it is bound to), try:

check_box("product", product.id, :checked => 'checked' )

Comment:

The rails documentation also mentions that you can use [] in the object name to automatically put the ID of the object in the name of the newly created element.

<%= check_box "message[]","active" %>

category: Howto