How to make a check_box checked in rails?

user1779563 picture user1779563 · Nov 29, 2012 · Viewed 59.4k times · Source

I made checkboxes using the following rails form helper:

<%= check_box("tag", tag.id) %>

However, I need to make some of them checked by default. The rails documentation doesn't specify how to do this. Is there a way? How?

Answer

John Bachir picture John Bachir · Feb 15, 2014

This has a very straightforward solution that is directly supported by check_box (at least as of rails 4, I didn't check older documentation)

<%= check_box("tag", tag.id, {checked: true}) %>

This will make the checkbox checked. Of course instead of true you will put in some logic which determines if each one is checked.