Rails - Display all questions by category
<p>I have question and category model. Question model has category_id column.</p>

<pre><code>class Question
  belongs_to :category
end
class Category
  has_many :questions
end
</code></pre>

<p>In my controller I have this: </p>

<pre><code>def index
   @categories = Category.all
   @questions = Question.all
end
</code></pre>

<p>I would like to display all categories and all questions that belongs_to specified category. Also, I would like to display question numbers below each category and made links of them and later it will open new page with clicked question. </p>

<p>This is how I tried to do that: </p>

<pre><code>&lt;% @categories.each do |category| %&gt;
    &lt;h1&gt;&lt;%= category.name %&gt;&lt;/h1&gt;
    &lt;% @questions.each do |question| %&gt;
         &lt;ul&gt;
             &lt;li&gt;&lt;%= link_to question.id %&gt;&lt;/li&gt;
         &lt;/ul&gt;
    &lt;% end %&gt;
&lt;% end %&gt;
</code></pre>

<p><br>It should look like this but I get stuck: </p>

<blockquote>
  <p>Category1<br>
  1     2     3     4</p>
  
  <p>Category2<br>
  1     2     3     4</p>
</blockquote>

<p>Question: How to achieve that I display questions like is show above? </p>