QandAシステムを作成しています。質問に対して、回答を作成するテストでエラーが発生します。
minitest エラー内容
ERROR["test_should_create_answer", #<Minitest::Reporters::Suite:0x00005595d9a180a0 @name="AnswersControllerTest">, 0.2696432336233556] test_should_create_answer#AnswersControllerTest (0.27s) ActionController::UrlGenerationError: ActionController::UrlGenerationError: No route matches {:action=>"index", :controller=>"answers"}, missing required keys: [:question_id] test/controllers/answers_controller_test.rb:21:in `block (2 levels) in <class:AnswersControllerTest>' test/controllers/answers_controller_test.rb:19:in `block in <class:AnswersControllerTest>'
minitest
1test "should create answer" do 2 assert_difference 'Answer.count' do 3 # debugger 4 post question_answers_path, params: { answer: { body: @answer.body, question_id: @answer.question_id, user_id: @answer.user_id } } 5 end 6 7 assert_redirected_to answer_url(Answer.last) 8 end
@answer.question_idの中身はちゃんと存在していました。なぜこのエラーが発生するのでしょうか?
routes.rb
```
resources :questions, shallow: true do
resources :answers
end
```
controller
```
class AnswersController < ApplicationController
before_action :set_answer, only: [:show, :edit, :update, :destroy]
# GET /answers
# GET /answers.json
def index
@questions = Question.all
end
# GET /answers/1
# GET /answers/1.json
def show
end
# GET /answers/new
def new
question = Question.find(params[:question_id])
@answer = question.answers.build
# @answer = question.answers.build(user_id: current_user.id)
end
# GET /answers/1/edit
def edit
end
# POST /answers
# POST /answers.json
def create
question = Question.find(params[:question_id])
@answer = question.answers.build(answer_params)
respond_to do |format|
if @answer.save
format.html { redirect_to @answer, notice: 'Answer was successfully created.' }
format.json { render :show, status: :created, location: @answer }
else
format.html { render :new }
format.json { render json: @answer.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /answers/1
# PATCH/PUT /answers/1.json
def update
respond_to do |format|
if @answer.update(answer_params)
format.html { redirect_to @answer, notice: 'Answer was successfully updated.' }
format.json { render :show, status: :ok, location: @answer }
else
format.html { render :edit }
format.json { render json: @answer.errors, status: :unprocessable_entity }
end
end
end
# DELETE /answers/1
# DELETE /answers/1.json
def destroy
@answer.destroy
respond_to do |format|
format.html { redirect_to answers_url, notice: 'Answer was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_answer
@answer = Answer.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def answer_params
params.require(:answer).permit(:user_id, :question_id, :body)
end
end
```
new.html.erb
```
<h1>New Answer</h1>
<!-- 回答する際に質問を表示しておくと分かりやすそうなので配置 -->
<div>質問情報</div>
タイトル:<%= @answer.question.title %><br>
内容:<%= @answer.question.body %><br>
<%= form_with(model: @answer, url: question_answers_path,local: true) do |form| %>
<% if @answer.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@answer.errors.count, "error") %> prohibited this answer from being saved:</h2>
<ul>
<% @answer.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :body %>
<%= form.text_area :body %>
</div>
<!-- ここにもuser_idを入れておかないとparamsに入らない -->
<%# form.hidden_field :user_id, value: current_user.id %>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
<%= link_to 'Back', answers_path %>
```
プログラムは質問を編集して、codeで載せてください。
インデントがなくなるので読みにくいです
回答1件
あなたの回答
tips
プレビュー