views:

2894

answers:

1

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.

+2  A: 

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
wesgarrison
I'm not reading/writing check_box value to the db, so switching to check_box_tag and using the true switch solved the problem. Thanks!
daustin777
The _tag methods are for non-model forms. The non-_tag ones are.
wesgarrison