Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a check_box checked in rails?

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?

like image 436
user1779563 Avatar asked Nov 28 '12 23:11

user1779563


2 Answers

Here's how to do it 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.

like image 155
John Bachir Avatar answered Oct 05 '22 20:10

John Bachir


If you need the check_box to be checked on new, and correctly filled on edit you can do:

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

As I mentioned here

like image 23
GEkk Avatar answered Oct 05 '22 22:10

GEkk