With Rails form builder, is there a way to have a checked check box equal a false value? -


this using f.check_box method in rails form_for block.

the checkbox represents bool attribute on model. but, want checked equal false , unchecked equal true. checked (false) default value.

so if user leaves checked , submits form, model attribute false.

see the docs. should fine with

f.check_box :something, {}, 0, 1 

the 0 , 1 checked , unchecked values, respectively.

update: thought checkbox checked/unchecked correctly when given checked/unchecked values, if isn't case, can this:

f.check_box :something, { :checked => !@model.something }, 0, 1 

i.e. explicitly tell checkbox checked when attribute false, , unchecked when it's true.

however, it's getting bit confusing flip values. perhaps "judo" way of handling rename attribute, rails' default behavior of false = unchecked can used.

2nd update: semantics of checkbox clear: checked means true (hence rails' default behavior). if you're using checkbox "backwards" imagine there's issue domain model. if user checks box, user answering in affirmative (e.g. "yes, want newsletter" or "yes, want stay logged in").
if model has flip around, model's attribute backwards compared user intention , maybe double negative (e.g. "yes, want newsletter" becoming no_newsletter = false). imagine must simpler model in straightforward terms.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -