我試圖添加問題的答案。每個問題都有答案。我展示他們的評論頁面上,通過諧音,除了我不斷收到此錯誤:未定義的變量或方法添加問題的答案
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 %>
感謝您的幫助:)
我試過這樣做,但仍然說答案是未定義的。 – user2561901