0

Hi I want to show a checkbox in which if value is true then itshows unchecked and if value is false then it shows checked. I am having is_active field as boolean which accepts 0 or1 . So if employee is_active = 1 then my checkbox must show unchecked if value is 0 then it show checked. How to do this am not getting it. I have written like this:

<%= f.check_box :is_active %>

so for this if its 1 then shows checked which I dont want. Please help me out. Thanks in advance!

5 Answers 5

2

You can try this I hope this will help you.

## Please replace your @employee object with your object.

<%= f.check_box(:is_active, {checked: (@employee.is_active ? false : true) }) %>

and if you wanted to update the object with reverse way please see the following code, i.e. if you will checked it it will update it with 0 and if you unchecked it will update it with 1.

<%= f.check_box(:is_active, {checked: (@employee.is_active ? false : true) }, 0, 1) %>
Sign up to request clarification or add additional context in comments.

Comments

0

Try to do like this:

<%= f.check_box(:is_active, {}, 0, 1) %>

Hope this will solve your issue

Comments

0

try ternary operator to check true/false and then check/uncheck

<input type="checkbox" checked="<%= f.is_active ? 'checked' : 'unchecked'%>" />

2 Comments

Raw HTML is not recommended in Rails. Good to have some rails way.
@RAJ its not recommended...can be used based on few scenarios sometimes.at the end every code get converted to html.. :)
0

You can specify check/uncheck values as:

<%= f.check_box :is_active, {}, "0", "1" %>

Comments

0

Try this

<%= f.check_box :is_active, {}, "true", "false" %>

this should work fine.....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.