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

前端 未结 7 1124
攒了一身酷
攒了一身酷 2021-02-06 21:38

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 JavaScr

7条回答
  •  时光取名叫无心
    2021-02-06 22:23

    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
    

提交回复
热议问题