将 mongoid_slug 与 CanCan 的 load_and_authorize_resource 结合使用

发布于 2024-12-18 19:03:54 字数 5855 浏览 1 评论 0原文

我将 Rails 3.1.1 与 cancan、Mongoid 和 Mongoid_slug 一起使用。

我的模型使用的是 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

I'm using Rails 3.1.1 with cancan, Mongoid, and Mongoid_slug.

My model is using slugs, which seems to be working fine except when cancan tries to load the resource for the show/edit actions. Which results in this error.

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'

Page Model

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

Controller

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏日落 2024-12-25 19:03:54

CanCan 的另一种可能更好的方法是使用 :find_by 选项。

class PagesController < ApplicationController
  load_and_authorize_resource :find_by => :slug
  ...
end

Another, possibily better way w/ CanCan is to use the :find_by option.

class PagesController < ApplicationController
  load_and_authorize_resource :find_by => :slug
  ...
end
〆一缕阳光ご 2024-12-25 19:03:54

看起来查询正在寻找 id 为“test”的文档。您确定将正确的参数传递到显示/编辑方法中吗?

在你看来这可能是一个错误。

It looks like the query is looking for an document with id of 'test'. Are you sure the right paramaters are being passed into the show/edit methods?

It is probably an error in your view.

并安 2024-12-25 19:03:54

我只需要重写模型的 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

I just needed to override the Model's find method, as that's what CanCan is calling.

def self.find *args
  if args.length == 1 and not args[0].is_a? Symbol
    find_by_slug(*args) || super
  else
    super
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文