rails check_box_tag set checked with default value

Viewed 53833

I currently have a rails check_box_tag call that looks like

check_box_tag #{name}

I want to include a checked attribute, which I know I can do with

check_box_tag name, value, checked

But what if I want to set it to checked without explicitly specifying value (I just want to use the default). Or similarly, what if I wanted to specify html options without specifying the checked attribute. Is there a way to do this?

6 Answers

If you pass in a value of "1" to the value field, it will pass-on the value of the real state of the checkbox, regardless of the checked default value:

is_checked = <default boolean>

check_box_tag :show_defaults, '1', is_checked

(now always reflects user input)

Related