我正在使用带有cancan,Mongoid和Mongoid_slug的Rails 3.1.1。
我的模型正在使用slugs,这似乎工作正常,除非cancan尝试加载显示/编辑操作的资源。这导致了这个错误。
Mongoid::Errors::DocumentNotFound in PagesController#show
Document not found for class Page with id(s) test.
mongoid (2.3.4) lib/mongoid/criterion/inclusion.rb:151:in `block in execute_or_raise'
mongoid (2.3.4) lib/mongoid/criterion/inclusion.rb:149:in `tap'
mongoid (2.3.4) lib/mongoid/criterion/inclusion.rb:149:in `execute_or_raise'
mongoid (2.3.4) lib/mongoid/criterion/inclusion.rb:129:in `find'
mongoid (2.3.4) lib/mongoid/finders.rb:84:in `find'
cancan (1.6.7) lib/cancan/model_adapters/abstract_adapter.rb:20:in `find'
cancan (1.6.7) lib/cancan/controller_resource.rb:110:in `find_resource'
cancan (1.6.7) lib/cancan/controller_resource.rb:68:in `load_resource_instance'
cancan (1.6.7) lib/cancan/controller_resource.rb:32:in `load_resource'
cancan (1.6.7) lib/cancan/controller_resource.rb:25:in `load_and_authorize_resource'
cancan (1.6.7) lib/cancan/controller_resource.rb:10:in `block in add_before_filter'
activesupport (3.1.1) lib/active_support/callbacks.rb:421:in `_run__438852266152246612__process_action__2308888977565164453__callbacks'
activesupport (3.1.1) lib/active_support/callbacks.rb:386:in `_run_process_action_callbacks'
activesupport (3.1.1) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.1.1) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.1.1) lib/action_controller/metal/rescue.rb:17:in `process_action'
actionpack (3.1.1) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.1.1) lib/active_support/notifications.rb:53:in `block in instrument'
activesupport (3.1.1) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.1.1) lib/active_support/notifications.rb:53:in `instrument'
actionpack (3.1.1) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.1.1) lib/action_controller/metal/params_wrapper.rb:201:in `process_action'
actionpack (3.1.1) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.1.1) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.1.1) lib/action_controller/metal.rb:193:in `dispatch'
actionpack (3.1.1) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.1.1) lib/action_controller/metal.rb:236:in `block in action'
actionpack (3.1.1) lib/action_dispatch/routing/route_set.rb:65:in `call'
actionpack (3.1.1) lib/action_dispatch/routing/route_set.rb:65:in `dispatch'
actionpack (3.1.1) lib/action_dispatch/routing/route_set.rb:29:in `call'
rack-mount (0.8.3) lib/rack/mount/route_set.rb:152:in `block in call'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:96:in `block in recognize'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:96:in `optimized_each'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:95:in `recognize'
rack-mount (0.8.3) lib/rack/mount/route_set.rb:141:in `call'
actionpack (3.1.1) lib/action_dispatch/routing/route_set.rb:532:in `call'
mongoid (2.3.4) lib/rack/mongoid/middleware/identity_map.rb:33:in `block in call'
mongoid (2.3.4) lib/mongoid.rb:132:in `unit_of_work'
mongoid (2.3.4) lib/rack/mongoid/middleware/identity_map.rb:33:in `call'
warden (1.1.0) lib/warden/manager.rb:35:in `block in call'
warden (1.1.0) lib/warden/manager.rb:34:in `catch'
warden (1.1.0) lib/warden/manager.rb:34:in `call'
actionpack (3.1.1) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.3.5) lib/rack/etag.rb:23:in `call'
rack (1.3.5) lib/rack/conditionalget.rb:25:in `call'
actionpack (3.1.1) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.1.1) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.1.1) lib/action_dispatch/middleware/flash.rb:243:in `call'
rack (1.3.5) lib/rack/session/abstract/id.rb:195:in `context'
rack (1.3.5) lib/rack/session/abstract/id.rb:190:in `call'
actionpack (3.1.1) lib/action_dispatch/middleware/cookies.rb:331:in `call'
actionpack (3.1.1) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (3.1.1) lib/active_support/callbacks.rb:392:in `_run_call_callbacks'
activesupport (3.1.1) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.1.1) lib/action_dispatch/middleware/callbacks.rb:28:in `call'
actionpack (3.1.1) lib/action_dispatch/middleware/reloader.rb:68:in `call'
rack (1.3.5) lib/rack/sendfile.rb:101:in `call'
actionpack (3.1.1) lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
actionpack (3.1.1) lib/action_dispatch/middleware/show_exceptions.rb:47:in `call'
railties (3.1.1) lib/rails/rack/logger.rb:13:in `call'
rack (1.3.5) lib/rack/methodoverride.rb:24:in `call'
rack (1.3.5) lib/rack/runtime.rb:17:in `call'
activesupport (3.1.1) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.3.5) lib/rack/lock.rb:15:in `call'
actionpack (3.1.1) lib/action_dispatch/middleware/static.rb:53:in `call'
railties (3.1.1) lib/rails/engine.rb:456:in `call'
railties (3.1.1) lib/rails/railtie/configurable.rb:30:in `method_missing'
页面模型
class Page
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Slug
field :title, type:String
field :body, type:String
belongs_to :user
slug :title, :scope => :user
validates :title, :presence => true
validates :body, :presence => true
end
控制器
class PagesController < ApplicationController
load_and_authorize_resource
def index
respond_to do |format|
format.html # index.html.erb
format.json { render json: @pages }
end
end
def show
respond_to do |format|
format.html # show.html.erb
format.json { render json: @page }
end
end
...
end
答案 0 :(得分:2)
另一种可能更好的方法是使用:find_by选项。
class PagesController < ApplicationController
load_and_authorize_resource :find_by => :slug
...
end
答案 1 :(得分:0)
我只需要覆盖Model的find方法,因为CanCan正在调用它。
def self.find *args
if args.length == 1 and not args[0].is_a? Symbol
find_by_slug(*args) || super
else
super
end
end