2013-10-30 36 views
0

我有两个资源问题和答案。我在问题中嵌套了答案。我很困惑,因为当我在我的答案在问题索引/:ID /答案,如果我点击编辑按钮,我得到与导轨嵌套的问题

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

但我知道ID是什么,所以如果键入我的本地主机的浏览器问题/:ID /答案/:ID /编辑就带我到编辑页面

我的浏览器说,这

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) %> 

和我的问题控制器不知道,如果需要,虽然我_form的

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_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 for answers

<%= 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 %> 

比较遗憾的是时髦的问题,希望有人知道怎么了感谢

回答

0

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

edit_question_answer_path([@question, @answer]) 

错误是告诉你它的搜索一个idedit的答案,你可以从生成的url中看到第二个id为空,这是你的答案ID。

+0

得到它的工作这是工作正确的答案,你设置我在正确的道路,虽然agmin谢谢!我需要改变(@question,@answer)为(@question,答案) –

+0

太棒了,我不记得生成的路径助手会带两个参数还是只是一个数组 – agmin