anastasiaalt / quiz

Anastasia's Ruby on Rails Project
1 stars 0 forks source link

Nested Loops Two Levels Deep #3

Closed anastasiaalt closed 8 years ago

anastasiaalt commented 8 years ago

I am trying to get all the options for a particular question for a particular quiz (options<question<quiz) to display. I have googled and tried various different syntax for nested loops. I am able to get all the questions for a particular quiz to display successfully (questions<quiz) but not able to go one layer deeper.... Here is my latest failing attempt (I have tried 10 or so related syntax experiments to this, moving around the end, adding a for and removing a for, adding the instance or @ and taking away, etc). I understand the concept but just not the syntax....

<% @quiz.questions.each do |question| %>
  <p>
    <strong>Question Description</strong>
    <%= question.ask %>
  </p>
  <p>
    <strong>Question Explanation</strong>
    <%= question.explanation %>
  </p>
  <% for question.options do |option| %>
    <p>
      <strong>Option</strong>
        <%= option.name %>
    </p>
  <% end %>
<% end %>
Avizacherman commented 8 years ago

Anastasia,

You just have to be really specific when nesting each loops in Ruby. I tried this in place of the for loop you tried and it seemed to work:

  <% question.options.each do |option| %>
    <%= option.name %>
    <% end %>

Let me know if that helps.

anastasiaalt commented 8 years ago

Ammmmmmaaaaaazing! :) :+1: