0

リソースに関する質問と回答が 2 つあります。質問内にネストされた回答があります。混乱しています。質問/:id/回答の回答インデックスにいるときに、編集ボタンをクリックすると表示されるからです

Couldn't find Answer with id=edit [WHERE "answers"."question_id" = ?]

しかし、私はIDが何であるかを知っているので、ローカルホストブラウザの質問/:id/answers/:id/editに入力すると、編集ページに移動します

私のブラウザはこれを言います

localhost:3000/questions/2/answers//edit

ここに私の回答コントローラーのコピーがあります

class AnswersController < ApplicationController

  before_filter :find_the_question
  # GET /answers
  # GET /answers.json
  def index
    @answers = @question.answers
  end

  # GET /answers/1
  # GET /answers/1.json
  def show
    @answer = @question.answers.find(params[:id])
  end

  # GET /answers/new
  def new
    @answer = @question.answers.new
  end

  # GET /answers/1/edit
  def edit
    @answer = @question.answers.find(params[:id])
  end

  # POST /answers
  # POST /answers.json
  def create
    @answer = @question.answers.new(answer_params)

    respond_to do |format|
      if @answer.save
        format.html { redirect_to [@question, @answer], 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

  # PATCH/PUT /answers/1
  # PATCH/PUT /answers/1.json
  def update
    @answer = @question.answers.find(params[:id])
    respond_to do |format|
      if @answer.update_attributes(answer_params)
        format.html { redirect_to question_answer_path(@question), 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

  # DELETE /answers/1
  # DELETE /answers/1.json
  def destroy
    @answer = @question.answers.find(params[:id])
    @answer.destroy
    respond_to do |format|
      format.html { redirect_to question_answers_path(@question) }
      format.json { head :no_content }
    end
  end

  private

    def find_the_question
      @question = Question.find(params[:question_id])
    end

    # 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, :created_at, :updated_at)
    end
end

そして私の答えのインデックス

<h1>Listing answers</h1>

<table>
  <thead>
    <tr>
      <th>User</th>
      <th>Question</th>
      <th>Body</th>
      <th>Created</th>
      <th>Updated</th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <% @answers.each do |answer| %>
      <tr>
        <td><%= answer.user_id %></td>
        <td><%= answer.question_id %></td>
        <td><%= answer.body %></td>
        <td><%= answer.created_at %></td>
        <td><%= answer.updated_at %></td>
        <td><%= link_to 'Show', [@question, @answer] %></td>
        <td><%= link_to 'Edit', edit_question_answer_path(@question, @answer) %></td>
        <td><%= link_to 'Destroy', [@question, @answer], method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to 'New Answer', new_question_answer_path(@question) %>

私の質問コントローラーは必要かどうかわかりませんが

class QuestionsController < ApplicationController
  before_action :set_question, only: [:show, :edit, :update, :destroy]

  # GET /questions
  # GET /questions.json
  def index
    @questions = Question.all
  end

  # GET /questions/1
  # GET /questions/1.json
  def show
    @question = Question.find(params[:id])
    @answers = @question.answers
  end

  # GET /questions/new
  def new
    @question = Question.new

    respond_to do |format|
      format.html
      format.json { render json: @question}
    end
  end

  # GET /questions/1/edit
  def edit
    @question = Question.find(params[:id])
  end

  # POST /questions
  # POST /questions.json
  def create
    @question = Question.new(question_params)

    respond_to do |format|
      if @question.save
        format.html { redirect_to @question, notice: 'Question was successfully created.' }
        format.json { render action: 'show', status: :created, location: @question }
      else
        format.html { render action: 'new' }
        format.json { render json: @question.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /questions/1
  # PATCH/PUT /questions/1.json
  def update
    respond_to do |format|
      if @question.update(question_params)
        format.html { redirect_to @question, notice: 'Question was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @question.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /questions/1
  # DELETE /questions/1.json
  def destroy
    @question.destroy
    respond_to do |format|
      format.html { redirect_to questions_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_question
      @question = Question.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def question_params
      params.require(:question).permit(:name, :body, :question_type_id, :created_at, :updated_at,
                                      :description, :user_question_set_id, :priority_id)
    end
end

質問用の _form のコピー

<%= form_for(@question) do |f| %>
  <% if @question.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@question.errors.count, "error") %> prohibited this question from being saved:</h2>

      <ul>
      <% @question.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :question_type_id %><br>
    <%= f.number_field :question_type_id %>
  </div>
  <div class="field">
    <%= f.label :priority_id %><br>
    <%= f.number_field :priority_id %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

ここに回答用の私の _form のコピーがあります

<%= form_for([@question, @answer]) do |f| %>
  <% 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 |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :user_id %><br>
    <%= f.text_field :user_id %>
  </div>
  <div class="field">
    <%= f.label :question_id %><br>
    <%= f.text_field :question_id %>
  </div>
  <div class="field">
    <%= f.label :body %><br>
    <%= f.text_field :body %>
  </div>
  <div class="field">
    <%= f.label :created_at %><br>
    <%= f.date_select :created_at %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

ファンキーな質問で申し訳ありません。

4

1 に答える 1