Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails check_box_tag checked according boolean value

In my db, I've a boolean field: is_in_city.

In view, I try to set check_box_tag as:

= check_box_tag c.is_in_city

But it is never checked even if the db value is true. What is wrong?

I need to do such chekbox, which is no/is checked according to db boolean field value stored in the database. How can I do this? Also how can I set one more my property to checkbox?

like image 315
Valdis Azamaris Avatar asked Aug 14 '13 21:08

Valdis Azamaris


1 Answers

The proper use of the check_box_tag method is like this:

= check_box_tag :name, value, checked

Where value can be anything, checked (should be) a boolean.

In your case:

= check_box_tag :is_in_city, 1, c.is_in_city

Documentation here: http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-check_box_tag

like image 151
MrYoshiji Avatar answered Nov 11 '22 03:11

MrYoshiji