在Rails中创建wiki应用程序时出现answer_url错误

时间:2012-02-22 20:50:21

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.1

您好我正在尝试将wiki集成到我创建的社交网站。在集成它之前,我仍然在创建wiki。所以我先做了:

rails generate scaffold Question title:string body:text

rails generate scaffold Answer question_id:integer body:text

我的模型文件如下:

question.rb:

class Question < ActiveRecord::Base
     has_many :answers
     validates :title, :presence => true 
     validates :body, :presence => true
end

answer.rb

class Answer < ActiveRecord::Base
     belongs_to :question
     validates :question_id, :presence => true 
     validates :body, :presence => true
end

我的控制器文件如下:

question_controller.rb:

 class QuestionsController < ApplicationController
     def index
        @questions = Question.all

           respond_to do |format|
          format.html # index.html.erb
          format.json { render :json => @questions }
    end
 end

    def show
          @question = Question.find(params[:id])

          respond_to do |format|
              format.html # show.html.erb
              format.json { render :json => @question }
          end
    end

    def new
           @question = Question.new

           respond_to do |format|
                format.html # new.html.erb
                format.json { render :json => @question }
          end
   end

   def edit
          @question = Question.find(params[:id])
   end


  def create
         @question = Question.new(params[:question])

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

    def update
       @question = Question.find(params[:id])

       respond_to do |format|
           if @question.update_attributes(params[:question])
              format.html { redirect_to @question, :notice => 'Question was successfully updated.' }
               format.json { head :ok }
           else
                format.html { render :action => "edit" }
                format.json { render :json => @question.errors, :status => :unprocessable_entity }
           end
       end
    end
  end

answer_controller.rb是:

   class AnswersController < ApplicationController

      def new
        @answer = Answer.new

        respond_to do |format|
        format.html # new.html.erb
        format.json { render :json => @answer }
      end
   end

   def create
         @question = Question.find(params[:question_id]) 
         @answer = @question.answers.create!(params[:answer])

         respond_to do |format|
            if @answer.save
                format.html { redirect_to @answer, :notice => 'Answer was successfully created.' }
                format.json { render :json => @answer, :status => :created, :location => @answer }
            else
                format.html { render :action => "new" }
                format.json { render :json => @answer.errors, :status => :unprocessable_entity }
            end
          end
       end
   end

最后我的路线文件是:

的routes.rb

 SampleApp::Application.routes.draw do

     resources :questions do 
         resources :answers
     end
 ........................

问题的查看文件是:

show.html.erb:

     <%= render :partial => @question %>
     <%= link_to 'Edit', edit_question_path(@question) %> |
     <%= link_to 'Back', questions_path %>
     <br/> <br/>
     <h4>Answers</h4> 
     <div id="answers">
     <% @question.answers.each do |answer| %> 
    <%= div_for answer do %>
       <p> <strong>
      Posted <%= time_ago_in_words(answer.created_at) %>
       </strong>
       <br /> <%= h(answer.body) %> 
       </p>
    <% end %> 
     <% end %>
   </div>

   <%= form_for [@question, Answer.new] do |f| %> 
   <p>
   <%= f.label :body, "New Answer" %>
   <br />
   <%= f.text_area :body %> 
    </p>
   <p><%= f.submit "Add Answer" %></p> 
  <% end %>

当我尝试创建一个新问题时,不会给我任何错误。当我打开已经创建的问题时,它会显示内容但我在尝试为已创建的问题添加新答案时出错,它会给我以下错误:

   **NoMethodError in AnswersController#create**

        undefined method `answer_url' for #<AnswersController:0x103273c70>
        app/controllers/answers_controller.rb:18:in `create'
        app/controllers/answers_controller.rb:16:in `create'

   **Request**

   **Parameters:**

        {"question_id"=>"1",
         "commit"=>"Add Answer",
         "authenticity_token"=>"ttPniOSpgGDMRu7+OJW2eS5rJMV/+ivVNOeI+rWOUPY=",
         "utf8"=>"\342\234\223",
         "answer"=>{"body"=>"ok!"}}

所以它正在做的是添加问题的答案,但由于某种原因没有将其重定向到同一个问题页面。当我重新加载相同的问题时,我发现添加了新的答案。所以,请你帮我解释为什么会这样?我需要能够添加帖子的答案,答案应该出现而不重新加载或给我上面的错误。谢谢。

1 个答案:

答案 0 :(得分:2)

问题是redirect_to @answer

替换为redirect_to @question

谨防:location => @answer