This question relates to a RoR quiz app. I am looking to display the results of the quiz, showing both the correct answer as well as the user's answer.
Therefore, in my view, I need to loop through each question and show the question answer, as well as the user's answer. However, I am struggling trying to figure out how to correctly query the user's answer.
To query the user_answer, I need the response_set where the user_id = current_user.id and I need to pass in the question_id as it loops through the each loop. However, I'm stuck on how to accomplish this.
What do I need to do to show the user_answer in the view alongside the correct_answer?
I have 3 models:
Response_set
belongs_to :user
has_many :responses
Reponse
belongs_to :response_set
belongs_to :question
Question
has_many :responses
Table fields:
create_table :response_sets do |t|
t.integer :user_id
create_table :responses do |t|
t.integer :response_set_id
t.integer :question_id
t.integer :user_answer
create_table :questions do |t|
t.integer :correct_answer
Controller
@questions = Question.all
@user = current_user.id
View (just looping through the questions and not displaying the user's answer)
<% @questions.each do |question| %>
<%= question.correct_answer %>
<% end %>
current_usermethod or something? How do you know for which user to show the response?@user = current_user.id