rails/rails — Issue #3995: f.check_box do not work inverting the checked and unchecked value
Author: sobrinho
Created: 2011-12-15 13:58:50+00:00
State: open

Hello,

I want to invert the logic of _destroy attribute on a specific nested form and I found a bug.

See this input:

``` ruby
f.check_box :_destroy, {}, false, true
```

I need this:

* If check box is checked, keep the record on database
* If check box is unchecked, destroy the record

The issue happens on this piece:

``` ruby
def check_box_checked?(value, checked_value)
case value
when TrueClass, FalseClass
value
when NilClass
false
when Integer
value != 0
when String
value == checked_value
when Array
value.include?(checked_value)
else
value.to_i != 0
end
end
```

Note if the value is a boolean value, this code do not compare with checked value, just returns the value.

The same will happens if nil means checked, or 0 means checked.

Also, the else seems strange for me.

Why not compare with checked_value on all cases?

I didn't tested but this seems more obvious:

``` ruby
def check_box_checked?(value, checked_value)
if value.is_a?(Array)
value.include?(checked_value)
else
value == checked_value
end
end
```

What you think?