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

前端 未结 7 1100
攒了一身酷
攒了一身酷 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:10

    I was shocked that none of the answers solve 100% of the problem.
    As all suggested solutions will give checked result on edit form, even if the user unchecked the checkbox.

    <%= f.check_box :subscribe, checked: @event.new_record? || f.object.subscribe? %>

    Works for rails 4 +, not tested below.

提交回复
热议问题