1

I am a newbie, I want to know how to set the value for the check_box from '1' to 'Yes' and '0' to 'NO' in front end of ruby on rails?

  <%= f.check_box :RDSAP_Answer, id: "enr_rds_surv_rdsap_xref_answer_check" %>

Now, in the browser, If I check the box it's store the value '1' to the database and if unchecked it's store the null value in the database.

I want to store Yes for checked and No for unchecked in the database. Is it possible to do in RoR? or is it easiest in JavaScript? What is the easiest way to set the value. Thanks in advance!

7
  • 2
    This can certainly be done, but may I ask why you want to do this? You will have a much easier time if you stick with the conventions... Commented Nov 8, 2012 at 17:03
  • Thanks! Actually I have a Question and answer type and changing the answer fields depend upon the question like the answer fields are text_box and check_box. So, the check_box questions are like a yes or no question. Commented Nov 8, 2012 at 17:05
  • Why does it matter which values are stored in the database? Commented Nov 8, 2012 at 17:06
  • I'm not understanding, you can simply display 'Yes' or 'No' if that's what you really need. Why do you need to actually store the strings 'Yes' and 'No'? Commented Nov 8, 2012 at 17:07
  • Because I am displaying the values stored in the database in the table in different page. Commented Nov 8, 2012 at 17:07

1 Answer 1

2

You should leave the checkbox values intact and just use a helper to display the value as needed:

Answered: <%= object.RDSAP_answer ? 'Yes' : 'No' %>

If you need this often, add a method to the model for this:

def display_RDSAP_answer
  RDSAP_answer ? 'Yes' : 'No'
end

And in your view:

Answered: <%= object.display_RDSAP_answer %>
Sign up to request clarification or add additional context in comments.

5 Comments

'front end below' - what do you mean by this?
below the code of <%= f.check_box :RDSAP_Answer, id: "enr_rds_surv_rdsap_xref_answer_check" %> in the VIEW?
If you want to display the current value as a string, then by all means add it there. But you shouldn't need to since the check_box already represents this - more likely, you should just add a label after the check_box to make it clear that the check_box state determines if it's been answered.
Thanks a lot!!. But still am confusing. Am very new to Ruby on Rails.
Perhaps you should read a bit more about controllers and views, and try out some tutorials. Here's one to get you started...

Your Answer

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