Ruby on rails 使用Mongoid::Errors::DocumentNotFound rails 3.1处理嵌套资源中的异常

Ruby on rails 使用Mongoid::Errors::DocumentNotFound rails 3.1处理嵌套资源中的异常,ruby-on-rails,ruby-on-rails-3,exception-handling,mongoid,Ruby On Rails,Ruby On Rails 3,Exception Handling,Mongoid,我的应用程序中有\u controller.rb class ApplicationController < ActionController::Base protect_from_forgery rescue_from Mongoid::Errors::DocumentNotFound, :with => :render_not_found def render_not_found render :file => "#{Rails.root}/public

我的应用程序中有\u controller.rb

class ApplicationController < ActionController::Base
  protect_from_forgery
   rescue_from Mongoid::Errors::DocumentNotFound, :with => :render_not_found
  def render_not_found
   render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
  end
end
class PostsController < ApplicationController

end
问题是如果我在routes.rb中有这样的嵌套资源:

resources :users do
 resources :posts
end
我在posts\u controller.rb中有这个

class ApplicationController < ActionController::Base
  protect_from_forgery
   rescue_from Mongoid::Errors::DocumentNotFound, :with => :render_not_found
  def render_not_found
   render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
  end
end
class PostsController < ApplicationController

end

在您的控制器代码中

class Users::PostsController < ApplicationController

end
class用户::PostsController

您有
Users::Posts
,但没有在上面的路由中指定
PostsController
的位置

谢谢,我如何在routes.rb中指定PostsController的位置?谢谢。很抱歉,我已经编辑了我与
:controller=>'users/posts'
检查过的帖子,但不起作用:(尝试此操作:
resources:posts,:controller=>“users/posts”
为什么要将
PostsController
放在
users
文件夹中?如果没有必要的原因,请将其与
UsersController
一起放在controllers目录中。删除
users::
部分,然后尝试是否一切都正常。我已检查了目录用户的posts输出,但没有找到它works:(.问题不在于目录:(@JatinGanhotra我已经编辑了这个问题。它可以很好地处理
资源:用户
输出。如果我把
资源:用户做资源:posts end
。不起作用。谢谢你把
rake routes
的输出放在这里。