2

I'm new to Rails and have been plugging away at this problem without success. I have a form for users to submit answers to multiple choice questions through radio buttons, and I would like their chosen answers to show up again when they revisit the question or on page reload. Their answer is stored in the database, through the TestQuestions model, in an answer_id column. The following code makes the last radio button checked instead of their given answer, Any suggestions on how to make the default value their answer from the database?

As per my comment below, I think this may require jQuery/javascript to compare the input value in the html ie -

<input id="answer_id_5" type="radio" value ="5" name="answer_id"></input> 

to the database entry.

show.html.erb

<%= form_tag({controller: "test_questions", action: "update"}, method: "patch") do %>

  <% @test_question.question.answers.each do |answer| %>
    <li>
    <%= radio_button_tag "answer_id", answer.id, checked: @test_question.answer_id  %> # THE PROBLEM LINE 
        <%= answer.content %>
    </li>
  <% end %>
  <%= submit_tag "Next"  %>
<% end %>

test_questions_controller.rb

def show
  @test_question = @test_questions.find(params[:id])
end

1 Answer 1

1

You should do it like this:

<%= radio_button_tag "answer_id", answer.id, checked: @test_question.answer_id == answer.id %>

This:

@test_question.answer_id == answer.id

will return either true or false, thus checking or unchecking the radio button.

@test_question.answer_id returns an integer > 0, which is a truthy value, meaning you're checking all the radio buttons. Since only one radio button can be checked, it'll be the last one.

Sign up to request clarification or add additional context in comments.

4 Comments

Excellent, that is helping me move forward, thank you Mischa. The problem now is that the code is comparing one database value to another. @test_question.answer_id == answer.id is always true if the user answered the question correctly, so "checked" is always true. If they answered incorrectly, the "checked" is always false. I will need to somehow access the input radio button value. For example, from <input id="answer_id_5" type="radio" value="5" name = "answer_id"></input> get the value and compare it to the database. I'm not sure but I think this may need jQuery/javascript.
How do you store what the correct answer is? What other fields do you have in your database. Please add that information to your question, without it it cannot be answered.
Ok, so I placed the boolean expression <%= answer.id == @test_question.answer_id %> above the radio_button_tag to check the values, and it returns the correct sequence (false, true, false) in this case, so the code should work, but the button is still not getting marked. That's strange.
Ok, now it works. Restarted the computer. <%= radio_button_tag "answer_id", answer.id, answer.id == @test_question.answer_id %> is the working code. Thanks Mischa.

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.