我正在尝试添加问题的答案。每个问题都有一个答案。我通过部分在评论页面上显示它们,但我不断收到此错误:
undefined local variable or method `answer'
这是我的 answers_controller.rb 的一部分
class AnswersController < ApplicationController
before_action :set_answer, only: [:show, :edit, :update, :destroy]
def index
@question = Question.find params[:question_id]
@question.answers
end
def show
end
def new
@question = Question.find params[:question_id]
end
def edit
end
def create
@question = Question.find(params[:question_id])
@answer = @question.answers.create(answer_params)
respond_to do |format|
if @answer.save
format.html { redirect_to @comment, notice: 'Answer was successfully created.' }
format.json { render action: 'show', status: :created, location: @answer }
else
format.html { render action: 'new' }
format.json { render json: @answer.errors, status: :unprocessable_entity }
end
end
end
这是我的 _question.html.erb 部分,其中调用了部分答案:
<%=div_for(question) do %>
<div class="questioncontainer">
<p>
<%= question.body %>
<%= render :partial => @question.answers %>
<% if current_user == @comment.user %>
<div class="answercontainer">
<%= link_to 'Answer', new_question_answer_path(question)%>
</div>
</div>
</p>
<% end %>
<% end %>
最后,这是我的 _answer.html.erb 部分:
<%=div_for(answer) do %>
<div class="questioncontainer">
<p>
<%= answer.body %>
</p>
</div>
<% end %>
谢谢您的帮助 :)