How to validate multi-option checkboxes

Hi,

I am trying to create a form for a dating site where users can specify
the body types they seek in their ideal match (ex: Slender, slim,
average, fat, etc.). Since users may be attracted to more than one body
type, they are allowed to check multiple boxes.

In my SQL schema, I’ve defined this attribute as “ideal_body_type”. How
should I store and validate this aggregated data in my model. Also, if
an error occurs during validation, how do I make the checked values in
these checkboxes stay checked?

Thanks in advance.

-Jesse

I think the easiest thing to do is to have each attribute represented as
a column in your scheme. So instead of having one column,
“ideal_body_type” have one for skinny, fat, super man, each holding a
boolean value.

On validation, you can make sure atleast one of them is checked:

before_save :confirm_body_type

def body_type
flag - 0
[:skinny, :fat, :super_man].each |body_type|
flag += 1 unless send(body_type).nil?
end
return false if flag.zero? else true
end

ok, so the example is cracked but the idea is somewhere in there…

Jesse wrote:

Hi,

I am trying to create a form for a dating site where users can specify
the body types they seek in their ideal match (ex: Slender, slim,
average, fat, etc.). Since users may be attracted to more than one body
type, they are allowed to check multiple boxes.

In my SQL schema, I’ve defined this attribute as “ideal_body_type”. How
should I store and validate this aggregated data in my model. Also, if
an error occurs during validation, how do I make the checked values in
these checkboxes stay checked?

Thanks in advance.

-Jesse