我的 action
def node
@node = TopicNode.find(params[:id])
@topics = @node.topics.last_actived.fields_for_list.includes(:user).paginate(:page => params[:page],:per_page => 15)
drop_breadcrumb("社区", topics_path)
drop_breadcrumb("#{@node.name}")
render :action => "index" #, :stream => true
end
运行出现这个错误
Mongoid::Errors::DocumentNotFound in TopicsController#node
Problem:
Document(s) not found for class Topic with id(s) 1.
Summary:
When calling Topic.find with an id or array of ids, each parameter must match a document in the database or this error will be raised. The search was for the id(s): 1 ... (1 total) and the following ids were not found: 1.
Resolution:
Search for an id that is in the database or set the Mongoid.raise_not_found_error configuration option to false, which will cause a nil to be returned instead of raising this error when searching for a single id, or only the matched documents when searching for multiples.
程序好似把/topics/node1 的 1 参数用来搜索 Topic 了,但是我的 def node 里面没这句代码啊!然后我把 def node 里面的代码都删除了,还是有这个错误。找不到原因了。
@cantin 确定是/topics/node1,同 ruby-china 一样,我在学习 ruby-china,我的 route.rb
match "topics/node:id" => "topics#node", :as => :node_topics
match "topics/node:id/feed" => "topics#node_feed", :as => :feed_node_topics
match "topics/last" => "topics#recent", :as => :recent_topics
resources :topics do
member do
post :reply
post :favorite
post :follow
post :unfollow
end
collection do
get :no_reply
get :popular
get :feed
post :preview
end
resources :replies
end
Mongoid::Errors::DocumentNotFound (
Problem:
Document(s) not found for class Topic with id(s) 1.
Summary:
When calling Topic.find with an id or array of ids, each parameter must match a document in the database or this error will be raised. The search was for the id(s): 1 ... (1 total) and the following ids were not found: 1.
Resolution:
Search for an id that is in the database or set the Mongoid.raise_not_found_error configuration option to false, which will cause a nil to be returned instead of raising this error when searching for a single id, or only the matched documents when searching for multiples.):
mongoid (3.0.1) lib/mongoid/criteria.rb:550:in `check_for_missing_documents!'
mongoid (3.0.1) lib/mongoid/criteria.rb:177:in `execute_or_raise'
mongoid (3.0.1) lib/mongoid/criteria.rb:255:in `find'
mongoid (3.0.1) lib/mongoid/finders.rb:61:in `find'
cancan (1.6.8) lib/cancan/model_adapters/abstract_adapter.rb:20:in `find'
cancan (1.6.8) lib/cancan/controller_resource.rb:114:in `find_resource'
cancan (1.6.8) lib/cancan/controller_resource.rb:68:in `load_resource_instance'
cancan (1.6.8) lib/cancan/controller_resource.rb:32:in `load_resource'
cancan (1.6.8) lib/cancan/controller_resource.rb:25:in `load_and_authorize_resource'
cancan (1.6.8) lib/cancan/controller_resource.rb:10:in `block in add_before_filter'
activesupport (3.2.9) lib/active_support/callbacks.rb:429:in `_run__4281606112534886323__process_action__491492952496635975__callbacks'
activesupport (3.2.9) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.9) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.9) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.9) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.9) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.9) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.2.9) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.9) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.9) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.9) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.9) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
actionpack (3.2.9) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.9) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.9) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.9) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.9) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.9) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (3.2.9) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (3.2.9) lib/action_dispatch/routing/route_set.rb:36:in `call'
journey (1.0.4) lib/journey/router.rb:68:in `block in call'
journey (1.0.4) lib/journey/router.rb:56:in `each'
journey (1.0.4) lib/journey/router.rb:56:in `call'
actionpack (3.2.9) lib/action_dispatch/routing/route_set.rb:601:in `call'
mongoid (3.0.1) lib/rack/mongoid/middleware/identity_map.rb:33:in `block in call'
mongoid (3.0.1) lib/mongoid/unit_of_work.rb:39:in `unit_of_work'
mongoid (3.0.1) lib/rack/mongoid/middleware/identity_map.rb:33:in `call'
warden (1.1.1) lib/warden/manager.rb:35:in `block in call'
warden (1.1.1) lib/warden/manager.rb:34:in `catch'
warden (1.1.1) lib/warden/manager.rb:34:in `call'
actionpack (3.2.9) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.1) lib/rack/etag.rb:23:in `call'
rack (1.4.1) lib/rack/conditionalget.rb:25:in `call'
actionpack (3.2.9) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.9) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.9) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.1) lib/rack/session/abstract/id.rb:205:in `context'
rack (1.4.1) lib/rack/session/abstract/id.rb:200:in `call'
actionpack (3.2.9) lib/action_dispatch/middleware/cookies.rb:341:in `call'
actionpack (3.2.9) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.9) lib/active_support/callbacks.rb:405:in `_run__884117061507234179__call__1917505127064085281__callbacks'
activesupport (3.2.9) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.9) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.9) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.9) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.9) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.9) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.9) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.9) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.9) lib/rails/rack/logger.rb:32:in `call_app'
railties (3.2.9) lib/rails/rack/logger.rb:16:in `block in call'
activesupport (3.2.9) lib/active_support/tagged_logging.rb:22:in `tagged'
railties (3.2.9) lib/rails/rack/logger.rb:16:in `call'
quiet_assets (1.0.1) lib/quiet_assets.rb:20:in `call_with_quiet_assets'
actionpack (3.2.9) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.1) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.9) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.1) lib/rack/lock.rb:15:in `call'
actionpack (3.2.9) lib/action_dispatch/middleware/static.rb:62:in `call'
rack-cache (1.2) lib/rack/cache/context.rb:136:in `forward'
rack-cache (1.2) lib/rack/cache/context.rb:245:in `fetch'
rack-cache (1.2) lib/rack/cache/context.rb:185:in `lookup'
rack-cache (1.2) lib/rack/cache/context.rb:66:in `call!'
rack-cache (1.2) lib/rack/cache/context.rb:51:in `call'
railties (3.2.9) lib/rails/engine.rb:479:in `call'
railties (3.2.9) lib/rails/application.rb:223:in `call'
rack (1.4.1) lib/rack/content_length.rb:14:in `call'
railties (3.2.9) lib/rails/rack/log_tailer.rb:17:in `call'
thin (1.5.0) lib/thin/connection.rb:81:in `block in pre_process'
thin (1.5.0) lib/thin/connection.rb:79:in `catch'
thin (1.5.0) lib/thin/connection.rb:79:in `pre_process'
thin (1.5.0) lib/thin/connection.rb:54:in `process'
thin (1.5.0) lib/thin/connection.rb:39:in `receive_data'
eventmachine (1.0.0) lib/eventmachine.rb:187:in `run_machine'
eventmachine (1.0.0) lib/eventmachine.rb:187:in `run'
thin (1.5.0) lib/thin/backends/base.rb:63:in `start'
thin (1.5.0) lib/thin/server.rb:159:in `start'
rack (1.4.1) lib/rack/handler/thin.rb:13:in `run'
rack (1.4.1) lib/rack/server.rb:265:in `start'
railties (3.2.9) lib/rails/commands/server.rb:70:in `start'
railties (3.2.9) lib/rails/commands.rb:55:in `block in <top (required)>'
railties (3.2.9) lib/rails/commands.rb:50:in `tap'
railties (3.2.9) lib/rails/commands.rb:50:in `<top (required)>'
script/rails:6:in `require'
script/rails:6:in `<main>'
Rendered /Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/actionpack-3.2.9/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.1ms)
Rendered /Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/actionpack-3.2.9/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.8ms)
Rendered /Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/actionpack-3.2.9/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (10.6ms)
没搞过 cancan 很久了,印象中它是默认你是试用 restful 的 route,所以它会自己去拿 params[:id] 去找的好像。下面是 cancan 的源代码,你去掉 cancan 试试看?
def find_resource
if @options[:singleton] && parent_resource.respond_to?(name)
parent_resource.send(name)
else
if @options[:find_by]
if resource_base.respond_to? "find_by_#{@options[:find_by]}!"
resource_base.send("find_by_#{@options[:find_by]}!", id_param)
else
resource_base.send(@options[:find_by], id_param)
end
else
adapter.find(resource_base, id_param)
end
end
end
是不是 controller 里加了 load_and_authorize_resource。cancan 会自动生成 object。 https://github.com/ryanb/cancan/wiki/Authorizing-controller-actions
个人建议 功能一点点加。 1.不要把所有的 gem 都加上,只加上自己用到的,熟悉的,慢慢的,随着功能复杂了,知道的多了,再添加更多的 gem。 2.自己写,一点点增加功能,不要一下子就做的很复杂,从简单到复杂。先实现简单的效果,明白了原理,在实现复杂的。