我正在尝试添加问题的答案。每个问题都有答案。我在评论页面上通过部分显示它们,除了我不断收到此错误:
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 %>
感谢您的帮助:)
答案 0 :(得分:0)
通过answer
locals: {answer: @answer}
作为本地传递
答案 1 :(得分:0)
使用
<%= render question.answers %>
而不是
<%= render :partial => @question.answers %>
此处有更多信息:http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials