저는 레일을 처음 사용하고 조금 문제가 있습니다. 나는 받고있다부분에 정의되지 않은 로컬 변수
undefined local variable or method `answer'
내 _answer.html.erb 부분에 오류가 있습니다. 그것이 중요한 내 자원 경우
<%=div_for(answer) do %>
<div class="questioncontainer">
<p>
<%= answer.body %>
</p>
</div>
<% end %>
:
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
def update
respond_to do |format|
if @answer.update(answer_params)
format.html { redirect_to @answer, notice: 'Answer was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @answer.errors, status: :unprocessable_entity }
end
end
end
def destroy
@answer.destroy
respond_to do |format|
format.html { redirect_to answers_url }
format.json { head :no_content }
end
end
내 _answer.html.erb 파일 :
여기 내 answers_controller.rb입니다 답변은 자원에 중첩되어 질문입니다.
감사합니다.
/어떻게 대답을 렌더링합니까? –
어떤 작업을 수행 할 때 오류가 발생합니까? 무엇이 : set_answer는? – Fred
@DaveNewton <% = render "answers/answer"%>를 사용하여 내 _question.html.erb 파일에서 렌더링합니다. – user2736480