I’ve been assigned to fix some things in code that a previous developer created. He stored serialized checkbox values in the database and although the values are most certainly being store in the table as described, when the form comes up, the check boxes are not checked according to what was stored in the DB. I’m assuming that I need to unserialize that data before the form can know which checkboxes should be checked.
Is there a way to unserialize the exclusions data so that the following code will be able to assign a true to the checked attribute of the check_box_tag method?
<p><label for="supplemental_material_type_exclusions">Exclusions</label><br/>
<%= ApplicantBudgetlevel.find(:all).collect { |p|
if @supplemental_material_type.exclusions
checked = @supplemental_material_type.exclusions.include?(p.level)
else
checked = false
end
"#{check_box_tag('supplemental_material_type[exclusions][]', p.level, checked)} #{p.level} - #{p.range}<br />\n"
} %>
</p>
I think the problem is a matter of scope; the
checkedvariable only exists in one of the branches of theifand does not exist once theendis reached. Try this:I simply removed the
elsebranch and set the value tofalseby default.This might cause further failures if the check box should only be shown if the
ifbranch is true — in which case, try the following:Which of the two approaches is correct depends upon more than I can gauge from here — but I’d try the first one first, and look through the logs for new or different errors or warnings.