daustin777 daustin777 - 6 months ago 9
Ruby Question

How can I make a check box default to being "checked" in Rails 1.2.3?

How can I make a check box default to being "checked" when it is initially displayed?

I've not found a "Rails" way to do this (that works) so I did it with JavaScript. Is there a proper way to do this in Rails? I'm using Rails 1.2.3.

Answer

If you're using check_box in the context of a form, then the check box will show whatever value that field has.

@user = Person.find(:first)
@user.active = true
check_box 'user', 'active'  #=> will be checked by default

If you're using check_box_tag, the third parameter is the initial state (check_box_tag doc):

check_box_tag "active", 1, true