0

I have a project with posts, comments and questions. The comments belong to a post and the questions belong to a comment. I am trying to show all the questions that belong to a comment on a page. However, the index page does not display any of the questions. It does not give an error but is just blank.

Here is my questions_controller.rb:

   class QuestionsController < ApplicationController
 before_action :set_question, only: [:show, :edit, :update, :destroy]


def index
  @comment = Comment.find params[:comment_id]
  @comment.questions
end


def show
end


def new
  @comment = Comment.find params[:comment_id]
end


def edit
end


def create
  @comment = Comment.find(params[:comment_id])
  @question = @comment.questions.create(question_params)

  respond_to do |format|
    if @question.save
      format.html { redirect_to comment_questions_path, notice: 'Question was successfully created.' }
    format.json { render action: 'show', status: :created, location: comment_questions_path }
  else
    format.html { render action: 'new' }
    format.json { render json: @question.errors, status: :unprocessable_entity }
    end
  end
end

The index file calls a _question.html.erb partial:

<%=div_for(@question) do %>
<%= @question.body %>
<% end %>

The index.html.erb file:

<%= render "questions/question" %>

And finally the link to the index page looks like this:

<%= link_to 'View Questions', comment_questions_path(comment)%>

I have checked and the questions are saving to the db so that's not the problem. I really appreciate any help.

2 Answers 2

1

Your partial is using an undefined variable and this is your main problem. But you also shouldn't be making references to instance variables in partials since that increases coupling between your partial and the controller. Try this:

app/views/questions/_question.html.erb

<%= div_for(question) do %>
  <%= question.body %>
<% end %>

app/views/questions/index.html.erb

This is where the real trick is. By passing a collection into the partial we're able to automatically iterate through it while passing a local variable named question which is exactly what we wanted.

<%= render @questions %>

For more info on rendering collections with partials, refer to the Rails Guides page on Layouts and Rendering.

app/controllers/questions_controller.rb

class QuestionsController < ApplicationController
  before_action :set_question, only: [:show, :edit, :update, :destroy]

  def index
    @comment = Comment.find params[:comment_id]
    @questions = @comment.questions
  end
end
Sign up to request clarification or add additional context in comments.

1 Comment

Cool. Put in some additional explanation of the partial rendering magic.
0

You have not defined @question variable anywhere in controller and you are using it in view, so it will be blank and display blank.

Try this code in questions_controller.rb

class QuestionsController < ApplicationController
 before_action :set_question, only: [:show, :edit, :update, :destroy]

  def index
    @comment = Comment.find params[:comment_id]
    @questions = @comment.questions
  end
  ...
end

And use @questions variable in view.

3 Comments

Have y0u changed your view, just check in index.html.erb: <%= @questions.count %> If it is nil, then either u r not having any question belongs to that comments or u hv nt put associations properly.
Yep, changed the view. <%= @questions.count %> works and shows the # of questions correctly.
can you paste your content inside index.html.erb what code u r using to call _question partial.

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.