0

我有两个资源问题和答案。我有嵌套在问题中的答案。我很困惑,因为当我在 questions/:id/answers 的答案索引上时,如果我点击编辑按钮,我会得到

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

但我知道 id 是什么,所以如果输入我的本地主机浏览器 questions/: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

0

看起来您只需要更新您的编辑路径(注意[]):

edit_question_answer_path([@question, @answer])

该错误告诉您它正在搜索带有idof的答案edit,您可以从生成的 url 中看到它使第二个 id 为空,这是您的答案 id。

于 2013-10-30T21:26:03.387 回答