byCoder
byCoder

Reputation: 9184

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?

Upvotes: 11

Views: 20474

Answers (1)

MrYoshiji
MrYoshiji

Reputation: 54882

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

Upvotes: 24

Related Questions