Ruby on rails ActiveRecord::RecordNotFound无法';找不到';id'=新的
在我添加答案之前,它工作正常。我创造的问题没有任何错误,但现在我继续得到这个Ruby on rails ActiveRecord::RecordNotFound无法';找不到';id'=新的,ruby-on-rails,ruby,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 4,在我添加答案之前,它工作正常。我创造的问题没有任何错误,但现在我继续得到这个 找不到id=new的问题 当我创建嵌套资源时,是否把路由弄乱了 Rails.application.routes.draw do devise_for :users root 'home#index' get '/users/:id' => 'home#profile' resources :users resources :questions resources :answers
找不到id=new的问题
当我创建嵌套资源时,是否把路由弄乱了
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
完整跟踪:
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'
我相信这跟这条线有关
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
这将强制您的路径与特定操作关联
把它放在所有嵌套和资源之上它在这一行
<%= form_for(@question, url: new_question_path, html: { method: :put, :class => 'form-horizontal' }) do |f| %>
正如你所见,这不是你想要返回的路线
假设已经构建了@question
,但没有保存(就像标准的/new
请求一样),我认为Rails足够聪明,可以从以下内容中找到您想要的一切:
<%= form_for @question %>
。。。下面是您要更新现有版本的内容:
<%= form_for(@question, url: question_path(@question), html: { method: :put, :class => 'form-horizontal' }) do |f| %>
'form horizontal'})do | f |%>
我注意到的第一件事是:问题和:答案都在routes.rb中定义了两次。如果您想要像/questions/1/answers/1
@PJSCopeland这样的路由,请尝试取出第一个副本并保留嵌套的资源。我之前尝试过,它通常不起作用,这意味着您在路由中遇到了/questions/:id
上面的/questions/new
。您的应用程序将使用rake routes
中的第一个匹配行解析请求。然而-假设你问的是GET/questions/new
,那么根据你在这里列出的内容,应该可以解决questions#new
,所以我想知道你提出的请求是否不那么明显?通常这意味着你最终得到的是/questions/:id上面的/questions/new这正是发生的事情。它正在搜索id=new,而不是转到正常的new\u question\u路径
。但我真的不知道是什么导致了这个错误。我试图删除额外的重复资源,但仍显示错误。这是否意味着问题超出了路由。rb?如果您告诉我导致错误的请求地址,我将能够跟踪它…进入您的路由并手动键入post'questions/new'=>'questions#new'如果不起作用,您将强制路由与特定操作关联,这是新路由的问题你的控制器内部的操作也不起作用,我正在使用生成的脚手架代码。我没有改变任何东西,有时生成的路由会被现有资源弄乱。嗯,我不确定。希望你能找到答案,祝你好运!等待我想我知道发生了什么。您正在使用一个私有方法来查找您正在创建的问题的ID。这对于更新问题是有意义的,但当您创建新问题时,该ID不存在,因为它会导致您在“questions#new”方法中查找@questions时出错。
<%= form_for(@question, url: new_question_path, html: { method: :put, :class => 'form-horizontal' }) do |f| %>
PUT /questions/:id(.:format) questions#update
<%= form_for @question %>
<%= 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| %>