RubyDigger19
RubyDigger19

Reputation: 835

Rails - Display all questions by category

I have question and category model. Question model has category_id column.

class Question
  belongs_to :category
end
class Category
  has_many :questions
end

In my controller I have this:

def index
   @categories = Category.all
   @questions = Question.all
end

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.

This is how I tried to do that:

<% @categories.each do |category| %>
    <h1><%= category.name %></h1>
    <% @questions.each do |question| %>
         <ul>
             <li><%= link_to question.id %></li>
         </ul>
    <% end %>
<% end %>


It should look like this but I get stuck:

Category1
1 2 3 4

Category2
1 2 3 4

Question: How to achieve that I display questions like is show above?

Upvotes: 3

Views: 374

Answers (2)

Maxim Pontyushenko
Maxim Pontyushenko

Reputation: 3043

You can do it this way:

Controller:

def index
   @categories = Category.all
end

View:

<% @categories.each do |category| %>
    <h1><%= category.name %></h1>
    <% category.questions.each do |question| %>
         <ul>
             <li><%= link_to question.id, question_path %></li>
         </ul>
    <% end %>
<% end %>

Upvotes: 3

Parker
Parker

Reputation: 513

Since you said you want to display all of the categories, and the questions that belong to each of those categories, I'm assuming that the index action that you pasted in comes from your categories_controller.

One solution I can think of would be to change the instance variables inside your index. I don't really see a purpose for having the instance variable that references all of your Question objects. This is the one I'm talking about:

@questions = Question.all

Yea, get rid of that. You should be fine with just

@categories = Category.all

Since you want to display all of your categories, that instance variable is necessary. And since you mentioned you want to also display all of the questions that belong to each category, that instance variable is sufficient with the right view. You were on the right track, but instead, just use the @categories instance variable; forget about @questions. Here is what your view should probably look like (you were on the right track above):

<% @categories.each do |category| %>
   <h1><%= category.name %></h1>
      <% category.questions.each do |question| %>
      <ul>
         <li><%= link_to question.id, question_path %></li>
      </ul>
      <% end %>
<% end %>

Also note that in that first line of code, when you start a block, you don't need the <%= , You only need the <%. That's because that first line of the block is purely ruby in itself, it isn't actually getting printed to the resulting html. Hope I helped a little bit!

* Also: I saw another answer on here which is missing something: When you use the <%= link_to %> helper, you need to specify the first argument which is the resulting markup (In this case you wanted the question.id) , AND ALSO A SECOND ARGUMENT, which is the path for the link to follow *

Upvotes: 2

Related Questions