The only thing you have todo for a check_box is to give it a unique name and a correct value like:
	
<pre><code><%= check_box('item_'+i.to_s, 'checked', {}, item.name, '') %></code></pre>
	
so, you can read it's value in the action like:

<pre><code>checked = @params['item_'+i.to_s]['checked']</code></pre>
   
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:

h2. Model

h3. item.rb

<pre><code>
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
</code></pre>

h2. View

h3. list.rhtml

<pre><code>
<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>
</code></pre>

h2. Controller

h3. item_controller.rhtml

<pre><code>
#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
</code></pre>

category: Howto
