1

I'm having this issue where I want to display the correct answer in a check box in Rails, to no avail.

The structure is further complicated by the fact that my models are nested into one another so that Survey not only has_many, but also accepts_nested_attributes_for Questions and Questions has_many Choices similarly. The problem is that I'm simply not able to get the box checked for the correct answer.

The data are saved to the database as a boolean in a column called correct_choice and this code for the show action shows the check boxes, but the correct choice is not checked:

 <table class="col-md-10">
    <thead>
      <tr>
        <% for question in @survey.questions do  %>
            <li><%= h question.content %></li> 
              <% for choice in question.choices do %>
              <li><%= h choice.content %>
                  <%= h check_box(:choice, :correct_choice ) %>
              <% end %>
              </li>
            </li>
        <% end %>
      </tr>
    </thead>  
</table>

I've read the documentation, but I cannot figure out how to apply it to my case. Can anybody point me in the right direction?

EDIT

This is the HTML that I get out:

    <table class="col-md-10">

    <thead>
      <tr>

            <li>Is this a question?</li> 
              <li>I&#39;m not big. I&#39;m having a breakdown.
                  <input name="correct_choice[content]" type="hidden" value="0" /><input type="checkbox" value="1" name="correct_choice[content]" id="correct_answer_content" />
              <li>Come on, I&#39;m not big.
                  <input name="correct_choice[content]" type="hidden" value="0" /><input type="checkbox" value="1" name="correct_choice[content]" id="correct_choice_content" />
              <li>Who is hungry?
                  <input name="correct_choice[content]" type="hidden" value="0" /><input type="checkbox" value="1" name="correct_choice[content]" id="correct_choice_content" />
              <li>Pass the cheese, please.
                  <input name="correct_choice[content]" type="hidden" value="0" /><input type="checkbox" value="1" name="correct_choice[content]" id="correct_choice_content" />
              </li>
            </li>
      </tr>
    </thead>  
</table>
3
  • Do you want to make it a form? Or just display it as a checkbox? Commented Apr 24, 2015 at 19:54
  • No, I can display it as a form. It works without any problem simply by using the nested associations between the models, but somehow the check box fails me.
    – ljnissen
    Commented Apr 24, 2015 at 20:03
  • 1
    Can you show the html output? Render the page and inspect. Commented Apr 24, 2015 at 20:10

1 Answer 1

0

I figured it out thanks to advice from @davidwessman. What he told me was to display the html and inspect. I did and compared to the html from the edit action (the one that works).

It then became clear that I needed to use the check_box_tag command instead of check_box. This is the code that I now use:

  <thead>
    <tr>
      <% for question in @survey.questions do  %>

          <li><%= h question.content %></li> 
            <% for choice in question.choices do %>
            <li><%= h choice.content %>
                <%= check_box_tag "correct_choice", "yes", answer.correct_choice %>
            <% end %>
            </li>
          </li>
      <% end %>
    </tr>
  </thead>

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.