在添加答案腳手架之前,這是正常工作。我正在創建問題沒有任何錯誤,但現在我繼續得到這個 Couldn't find Question with 'id'=new
ActiveRecord :: 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'
我注意到的第一件事是':questions'和':答案'都在'routes.rb'中定義了兩次。如果你想要像'/ questions/1/answers/1'這樣的路線,試着取出第一份並留下嵌套的資源。 – PJSCopeland
@PJSCopeland我早些時候試過它並沒有工作 – Raymond
通常這意味着你最後在路由中的'/ questions /:'上面有'/ questions /:id'。您的應用程序將使用'rake routes'中的第一個匹配行來解析請求。然而 - 假設*你要求'GET/questions/new' - 這*應該*根據你在這裏列出的內容解析爲'questions#new',所以我想知道是否你的請求製作不太明顯? – PJSCopeland