svenfuchs
10/26/2010 - 2:20 PM

nested_attributes checkbox group


Given state:

  [x] Monitors
  [ ] Printers

Form:

  <input type="hidden"   name="categorizations_attributes[id]"       value="1">
  <input type="hidden"   name="categorizations_attributes[_destroy]" value="1">
  <input type="checkbox" name="categorizations_attributes[_destroy]" value="0" checked="checked">
  <input type="checkbox" name="categorizations_attributes[category_id]" value="2">

Just submitting the form:

  [x] Monitors
  [ ] Printers

  => { :categorizations_attributes => [{ :id => 1, :_destroy => 0 }] }

Removing a categorization:

  [ ] Monitors
  [ ] Printers

  => { :categorizations_attributes => [{ :id => 1, :_destroy => 1 }] }

Adding a categorization:

  [x] Monitors
  [x] Printers

  => { :categorizations_attributes => [{ :categories_id => 1 }] }