2016-02-23 121 views
1

在添加答案腳手架之前,這是正常工作。我正在創建問題沒有任何錯誤,但現在我繼續得到這個 Couldn't find Question with 'id'=newActiveRecord :: RecordNotFound找不到'id'=新

當我創建嵌套的資源時,我弄亂了路線嗎?

Rails.application.routes.draw do 
    devise_for :users 
    root 'home#index' 
    get '/users/:id' => 'home#profile' 
    resources :users 
    resources :questions 
    resources :answers 

    resources :questions do 
    resources :answers 
    end 
end 

答案控制器:

class AnswersController < ApplicationController 
    before_action :set_answer, only: [:destroy] 

    def create 
    @question = Question.find(params[:question_id]) 
    @answer = @question.answers.create(answer_params) 
    @answer.username = current_user.username 
    @answer.user_id = current_user.id 
    @answer.avatar = current_user.avatar 
    @answer.save! 
    redirect_to :back 
    end 

    def destroy 
    @answer.destroy 
    redirect_to :back 
    end 

    private 

    def set_answer 
    @answer = Answer.find(params[:id]) 
    end 

    def answer_params 
    params.require(:answer).permit(:body) 
    end 
end 

奇怪的是,該錯誤貴不貴控制器中,我還沒有觸及這一切。

問題控制器:

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

    respond_to :html 

    def index 
    @questions = Question.all 
    respond_with(@questions) 
    end 

    def show 
    respond_with(@question) 
    end 

    def new 
    if user_signed_in? 
     @question = current_user.questions.build 
     respond_with(@question) 
    else 
     redirect_to new_user_session_path 
    end 
    end 

    def edit 
    end 

    def create 
    @question = current_user.questions.build(question_params) 
    if @question.save 
     redirect_to question_path(@question) 
    else 
     render 'new' 
    end 
    end 

    def update 
    @question.update(question_params) 
    respond_with(@question) 
    end 

    def destroy 
    @question.destroy 
    respond_with(@question) 
    end 

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

    def question_params 
     params.require(:question).permit(:title, :description) 
    end 
end 

處理該問題提交形式:

<div class="signin"> 
    <div class="container"> 
    <div class="row"> 
     <div class="col-md-8"> 
     <div class="well boxShadow"> 
      <div class="media"> 
      <div class="media-body"> 
       <h4 class="media-heading red">New Question</h4> 
       <hr /> 
       <%= form_for(@question, url: new_question_path, html: { method: :put, :class => 'form-horizontal' }) do |f| %> 

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

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

        <div class="form-group"> 
        <div class="col-sm-12"> 
        <%= f.text_field :title, :class => "form-control outline", :placeholder => "Enter your question" %> 
        </div> 
        </div> 

        <div class="form-group"> 
        <div class="col-sm-12"> 
        <%= f.text_area :description, :class => "form-control outline", rows: 15 %> 
        </div> 
        </div>     

        <div class="form-group"> 
        <div class="col-sm-12"> 
         <%= f.submit "Ask", :class => "btn btn-danger btn-md btn-block" %> 
        </div> 
        </div> 
       <% end %> 
      </div> 
      </div> 
     </div> 
     </div> 
    </div> 
</div> 

耙路線:

    Prefix Verb URI Pattern 
    Controller#Action 
       answers GET /answers(.:format) 
    answers#index 
         POST /answers(.:format) 
    answers#create 
       new_answer GET /answers/new(.:format) 
    answers#new 
      edit_answer GET /answers/:id/edit(.:format) 
    answers#edit 
        answer GET /answers/:id(.:format) 
    answers#show 
         PATCH /answers/:id(.:format) 
    answers#update 
         PUT /answers/:id(.:format) 
    answers#update 
         DELETE /answers/:id(.:format) 
    answers#destroy 
     question_answers GET /questions/:question_id/answers(.:format) 
    answers#index 
         POST /questions/:question_id/answers(.:format) 
    answers#create 
    new_question_answer GET /questions/:question_id/answers/new(.:format) 
    answers#new 
    edit_question_answer GET /questions/:question_id/answers/:id/edit(.:form 
t) answers#edit 
     question_answer GET /questions/:question_id/answers/:id(.:format) 
    answers#show 
         PATCH /questions/:question_id/answers/:id(.:format) 
    answers#update 
         PUT /questions/:question_id/answers/:id(.:format) 
    answers#update 
         DELETE /questions/:question_id/answers/:id(.:format) 
    answers#destroy 
       questions GET /questions(.:format) 
    questions#index 
         POST /questions(.:format) 
    questions#create 
      new_question GET /questions/new(.:format) 
    questions#new 
      edit_question GET /questions/:id/edit(.:format) 
    questions#edit 
       question GET /questions/:id(.:format) 
    questions#show 
         PATCH /questions/:id(.:format) 
    questions#update 
         PUT /questions/:id(.:format) 
    questions#update 
         DELETE /questions/:id(.:format) 
    questions#destroy 
     new_user_session GET /users/sign_in(.:format) 
    devise/sessions#new 
      user_session POST /users/sign_in(.:format) 
    devise/sessions#create 
    destroy_user_session DELETE /users/sign_out(.:format) 
    devise/sessions#destroy 
      user_password POST /users/password(.:format) 
    devise/passwords#create 
     new_user_password GET /users/password/new(.:format) 
    devise/passwords#new 
     edit_user_password GET /users/password/edit(.:format) 
    devise/passwords#edit 
         PATCH /users/password(.:format) 
    devise/passwords#update 
         PUT /users/password(.:format) 
    devise/passwords#update 
cancel_user_registration GET /users/cancel(.:format) 
    devise/registrations#cancel 
     user_registration POST /users(.:format) 
    devise/registrations#create 
    new_user_registration GET /users/sign_up(.:format) 
    devise/registrations#new 
    edit_user_registration GET /users/edit(.:format) 
    devise/registrations#edit 
         PATCH /users(.:format) 
    devise/registrations#update 
         PUT /users(.:format) 
    devise/registrations#update 
         DELETE /users(.:format) 
    devise/registrations#destroy 
        root GET /
    home#index 
         GET /users/:id(.:format) 
    home#profile 
        users GET /users(.:format) 
    users#index 
         POST /users(.:format) 
    users#create 
       new_user GET /users/new(.:format) 
    users#new 
       edit_user GET /users/:id/edit(.:format) 
    users#edit 
        user GET /users/:id(.:format) 
    users#show 
         PATCH /users/:id(.:format) 
    users#update 
         PUT /users/:id(.:format) 
    users#update 
         DELETE /users/:id(.:format) 
    users#destroy 
         GET /questions(.:format) 
    questions#index 
         POST /questions(.:format) 
    questions#create 
         GET /questions/new(.:format) 
    questions#new 
         GET /questions/:id/edit(.:format) 
    questions#edit 
         GET /questions/:id(.:format) 
    questions#show 
         PATCH /questions/:id(.:format) 
    questions#update 
         PUT /questions/:id(.:format) 
    questions#update 
         DELETE /questions/:id(.:format) 
    questions#destroy 

完全跟蹤:

activerecord (4.1.8) lib/active_record/relation/finder_methods.rb:320:in `raise_record_not_found_exception!' 
activerecord (4.1.8) lib/active_record/relation/finder_methods.rb:429:in `find_one' 
activerecord (4.1.8) lib/active_record/relation/finder_methods.rb:413:in `find_with_ids' 
activerecord (4.1.8) lib/active_record/relation/finder_methods.rb:68:in `find' 
C:in `find' 
app/controllers/questions_controller.rb:48:in `set_question' 
activesupport (4.1.8) lib/active_support/callbacks.rb:424:in `block in make_lambda' 
activesupport (4.1.8) lib/active_support/callbacks.rb:143:in `call' 
activesupport (4.1.8) lib/active_support/callbacks.rb:143:in `block in halting_and_conditional' 
activesupport (4.1.8) lib/active_support/callbacks.rb:229:in `call' 
activesupport (4.1.8) lib/active_support/callbacks.rb:229:in `block in halting' 
activesupport (4.1.8) lib/active_support/callbacks.rb:149:in `call' 
activesupport (4.1.8) lib/active_support/callbacks.rb:149:in `block in halting_and_conditional' 
activesupport (4.1.8) lib/active_support/callbacks.rb:229:in `call' 
activesupport (4.1.8) lib/active_support/callbacks.rb:229:in `block in halting' 
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `call' 
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting' 
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `call' 
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting' 
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `call' 
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting' 
activesupport (4.1.8) lib/active_support/callbacks.rb:86:in `call' 
activesupport (4.1.8) lib/active_support/callbacks.rb:86:in `run_callbacks' 
actionpack (4.1.8) lib/abstract_controller/callbacks.rb:19:in `process_action' 
actionpack (4.1.8) lib/action_controller/metal/rescue.rb:29:in `process_action' 
actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action' 
activesupport (4.1.8) lib/active_support/notifications.rb:159:in `block in instrument' 
activesupport (4.1.8) lib/active_support/notifications/instrumenter.rb:20:in `instrument' 
activesupport (4.1.8) lib/active_support/notifications.rb:159:in `instrument' 
actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:30:in `process_action' 
actionpack (4.1.8) lib/action_controller/metal/params_wrapper.rb:250:in `process_action' 
activerecord (4.1.8) lib/active_record/railties/controller_runtime.rb:18:in `process_action' 
actionpack (4.1.8) lib/abstract_controller/base.rb:136:in `process' 
actionview (4.1.8) lib/action_view/rendering.rb:30:in `process' 
actionpack (4.1.8) lib/action_controller/metal.rb:196:in `dispatch' 
actionpack (4.1.8) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch' 
actionpack (4.1.8) lib/action_controller/metal.rb:232:in `block in action' 
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:82:in `call' 
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:82:in `dispatch' 
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:50:in `call' 
actionpack (4.1.8) lib/action_dispatch/journey/router.rb:73:in `block in call' 
actionpack (4.1.8) lib/action_dispatch/journey/router.rb:59:in `each' 
actionpack (4.1.8) lib/action_dispatch/journey/router.rb:59:in `call' 
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:678:in `call' 
warden (1.2.6) lib/warden/manager.rb:35:in `block in call' 
warden (1.2.6) lib/warden/manager.rb:34:in `catch' 
warden (1.2.6) lib/warden/manager.rb:34:in `call' 
rack (1.5.5) lib/rack/etag.rb:23:in `call' 
rack (1.5.5) lib/rack/conditionalget.rb:35:in `call' 
rack (1.5.5) lib/rack/head.rb:11:in `call' 
actionpack (4.1.8) lib/action_dispatch/middleware/params_parser.rb:27:in `call' 
actionpack (4.1.8) lib/action_dispatch/middleware/flash.rb:254:in `call' 
rack (1.5.5) lib/rack/session/abstract/id.rb:225:in `context' 
rack (1.5.5) lib/rack/session/abstract/id.rb:220:in `call' 
actionpack (4.1.8) lib/action_dispatch/middleware/cookies.rb:560:in `call' 
activerecord (4.1.8) lib/active_record/query_cache.rb:36:in `call' 
activerecord (4.1.8) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call' 
activerecord (4.1.8) lib/active_record/migration.rb:380:in `call' 
actionpack (4.1.8) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call' 
activesupport (4.1.8) lib/active_support/callbacks.rb:82:in `run_callbacks' 
actionpack (4.1.8) lib/action_dispatch/middleware/callbacks.rb:27:in `call' 
actionpack (4.1.8) lib/action_dispatch/middleware/reloader.rb:73:in `call' 
actionpack (4.1.8) lib/action_dispatch/middleware/remote_ip.rb:76:in `call' 
actionpack (4.1.8) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call' 
actionpack (4.1.8) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call' 
railties (4.1.8) lib/rails/rack/logger.rb:38:in `call_app' 
railties (4.1.8) lib/rails/rack/logger.rb:20:in `block in call' 
activesupport (4.1.8) lib/active_support/tagged_logging.rb:68:in `block in tagged' 
activesupport (4.1.8) lib/active_support/tagged_logging.rb:26:in `tagged' 
activesupport (4.1.8) lib/active_support/tagged_logging.rb:68:in `tagged' 
railties (4.1.8) lib/rails/rack/logger.rb:20:in `call' 
actionpack (4.1.8) lib/action_dispatch/middleware/request_id.rb:21:in `call' 
rack (1.5.5) lib/rack/methodoverride.rb:21:in `call' 
rack (1.5.5) lib/rack/runtime.rb:17:in `call' 
activesupport (4.1.8) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call' 
rack (1.5.5) lib/rack/lock.rb:17:in `call' 
actionpack (4.1.8) lib/action_dispatch/middleware/static.rb:84:in `call' 
rack (1.5.5) lib/rack/sendfile.rb:112:in `call' 
railties (4.1.8) lib/rails/engine.rb:514:in `call' 
railties (4.1.8) lib/rails/application.rb:144:in `call' 
rack (1.5.5) lib/rack/lock.rb:17:in `call' 
rack (1.5.5) lib/rack/content_length.rb:14:in `call' 
rack (1.5.5) lib/rack/handler/webrick.rb:60:in `service' 
C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service' 
C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run' 
C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread' 
+0

我注意到的第一件事是':questions'和':答案'都在'routes.rb'中定義了兩次。如果你想要像'/ questions/1/answers/1'這樣的路線,試着取出第一份並留下嵌套的資源。 – PJSCopeland

+0

@PJSCopeland我早些時候試過它並沒有工作 – Raymond

+0

通常這意味着你最後在路由中的'/ questions /:'上面有'/ questions /:id'。您的應用程序將使用'rake routes'中的第一個匹配行來解析請求。然而 - 假設*你要求'GET/questions/new' - 這*應該*根據你在這裏列出的內容解析爲'questions#new',所以我想知道是否你的請求製作不太明顯? – PJSCopeland

回答

2

這是在這條線上面這裏。

<%= form_for(@question, url: new_question_path, html: { method: :put, :class => 'form-horizontal' }) do |f| %> 

之間new_question_pathmethod: :put,形式指定信息應在PUT /questions/new被返回。

下面是rake routes第一線相匹配的:

PUT /questions/:id(.:format) questions#update 

正如你所看到的,這不是你想返回的路線。

假設@question已建成但尚未保存(因爲這將是在標準/new要求),我認爲 Rails是一個足夠聰明弄清楚你只是這個想要的一切:

<%= form_for @question %> 

(尤其是如果您在部分中使用此表單時,也會從/edit請求中加載)。

否則,您將需要兩個不同的調用。這裏是你想要創建一個新的形式:

<%= form_for(@question, url: questions_path, html: { method: :post, :class => 'form-horizontal' }) do |f| %> 

...這是你想要什麼更新現有的:

<%= form_for(@question, url: question_path(@question), html: { method: :put, :class => 'form-horizontal' }) do |f| %> 
+0

你先生是一種生活品味。第二個訣竅。 – Raymond

0

我相信這與該行

def create 
    @question = Question.find(params[:question_id]) <================== 
    @answer = @question.answers.create(answer_params) 
    @answer.username = current_user.username 
    @answer.user_id = current_user.id 
    @answer.avatar = current_user.avatar 
    @answer.save! 
    redirect_to :back 
end 

Rails正在試圖找到您要添加的問題的答案做。雖然它無法找到問題,因爲發送的ID不正確。它接收'新',而不是1,2,3.

我會嘗試的第一件事情是將嵌套路線置於默認資源之上。路線有時事項,並可以檢查默認resources :questions代替嵌套路線

的順序試一下,我會繼續看到,如果我能想到的其他什麼東西,但是這是我注意到的第一件事。

編輯:

順便說一句,我只是檢查你的耙路線的底部,問題#新的不應該是在底部。默認的資源應該總是在上面,因爲它會找到第一個合法的路由(questons /:id/answers/new),並且會忘記下面的路由。因此,請嘗試對它們進行重新排序,以使問題#新在您的耙路線中首先顯示。

編輯2:很抱歉,如果我的意見的格式是怪異

型到這一點你的路由

POST 'questions/new' => 'questions#new' 

這將迫使你的路徑與特定操作相關聯。

把它上面的所有嵌套和資源

+0

可悲的是,它沒有工作 – Raymond

+0

進入你的路線和手動輸入 後的提問/新'=>「問題#新」 你會被強迫與特定操作相關聯 如果路線不起作用這是你的控制器內部的新動作的問題 –

+0

也沒有工作,我正在使用生成的腳手架代碼。我沒有改變任何東西 – Raymond

相關問題