无法将CanCanCan与ActiveAdmin一起使用并设计未定义的方法`仪表板&#39;对于#<adminuser>

时间:2017-11-03 11:52:08

标签: ruby-on-rails devise ruby-on-rails-5 activeadmin cancancan

我正在使用有效的管理员构建管理面板 我已经安装了三个宝石

gem 'activeadmin', github: 'activeadmin'
gem 'devise'
gem 'cancancan'

我将其设置如下:

rails g active_admin:install # Created a nice admin panel, with AdminUser Model
rails generate active_admin:resource MyModel # Added all my models

配置/ active_admin.rb

config.authorization_adapter = ActiveAdmin::CanCanAdapter
config.cancan_ability_class = Ability
config.authentication_method = :authenticate_admin_user!  

ability.rb

class Ability
  include CanCan::Ability

  def initialize(user)
    if user.admin?
      can :read, ActiveAdmin::Page, :name => "Dashboard"
    else
      can :read, ActiveAdmin::Page, :name => "Dashboard"
    end
  end
end

我还使用formtastic定义了很好的自定义方法,比如我为活动管理员生成的所有资源模型的索引,显示,编辑等方法。

例如,我的admin / dashboard.rb

ActiveAdmin.register_page "Dashboard" do

  menu priority: 1, label: proc{ I18n.t("active_admin.dashboard") }

  # some tables to display
  content title: proc{ I18n.t("active_admin.dashboard") } do
    div class: "blank_slate_container", id: "dashboard_default_message" do
      columns do
        column do
          panel "Latest Visitors" do
            # table_for Visitor('id desc').limit(10) do
            table_for Patient.order("id desc").limit(10) do
              column(:name) 
              column(:mobile)
            end
          end
        end
      end # columns
    end
  end # content

  controller do
    # before_action :authenticate_admin_user!   
    load_and_authorize_resource :through => :current_admin_user
  end

end

如果我注释掉load_and_authorize_resource这个不错的登录系统,该应用程序运行正常,该系统会在成功登录详细信息时显示仪表板。 但是当我使用&#34; load_and_authorize_resource&#34;时,我加载https://mywebsite/admin时会抛出错误

NoMethodError in Admin::DashboardController#index
undefined method `dashboards' for #<AdminUser:0x007f0afd642878>

方法初始化似乎工作正常。但它不知何故没有正确地重定向到仪表板。 我没有专门使用设计,但是当我最初运行rails g active_admin:install时,在设置activeadmin时,它会根据activeadmin文档自动使用设计。

更新堆栈跟踪:

Processing by Admin::DashboardController#index as HTML
  AdminUser Load (50.8ms)  SELECT  "admin_users".* FROM "admin_users" WHERE "admin_users"."id" = $1 ORDER BY "admin_users"."id" ASC LIMIT $2  [["id", 2], ["LIMIT", 1]]
Completed 500 Internal Server Error in 106ms (ActiveRecord: 50.8ms)



NoMethodError (undefined method `dashboards' for #<AdminUser:0x000000063c4fa0>):

activemodel (5.0.6) lib/active_model/attribute_methods.rb:433:in `method_missing'
cancancan (2.0.0) lib/cancan/controller_resource.rb:201:in `resource_base_through'
cancancan (2.0.0) lib/cancan/controller_resource.rb:190:in `resource_base'
cancancan (2.0.0) lib/cancan/controller_resource.rb:72:in `load_collection?'
cancancan (2.0.0) lib/cancan/controller_resource.rb:36:in `load_resource'
cancancan (2.0.0) lib/cancan/controller_resource.rb:28:in `load_and_authorize_resource'
cancancan (2.0.0) lib/cancan/controller_resource.rb:12:in `block in add_before_action'
activesupport (5.0.6) lib/active_support/callbacks.rb:398:in `instance_exec'
activesupport (5.0.6) lib/active_support/callbacks.rb:398:in `block in make_lambda'
activesupport (5.0.6) lib/active_support/callbacks.rb:169:in `block (2 levels) in halting'
actionpack (5.0.6) lib/abstract_controller/callbacks.rb:12:in `block (2 levels) in <module:Callbacks>'
activesupport (5.0.6) lib/active_support/callbacks.rb:170:in `block in halting'
activesupport (5.0.6) lib/active_support/callbacks.rb:454:in `block in call'
activesupport (5.0.6) lib/active_support/callbacks.rb:454:in `each'
activesupport (5.0.6) lib/active_support/callbacks.rb:454:in `call'
activesupport (5.0.6) lib/active_support/callbacks.rb:101:in `__run_callbacks__'
activesupport (5.0.6) lib/active_support/callbacks.rb:750:in `_run_process_action_callbacks'
activesupport (5.0.6) lib/active_support/callbacks.rb:90:in `run_callbacks'
actionpack (5.0.6) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (5.0.6) lib/action_controller/metal/rescue.rb:20:in `process_action'
actionpack (5.0.6) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (5.0.6) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (5.0.6) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.0.6) lib/active_support/notifications.rb:164:in `instrument'
actionpack (5.0.6) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (5.0.6) lib/action_controller/metal/params_wrapper.rb:248:in `process_action'
activerecord (5.0.6) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (5.0.6) lib/abstract_controller/base.rb:126:in `process'
actionview (5.0.6) lib/action_view/rendering.rb:30:in `process'
actionpack (5.0.6) lib/action_controller/metal.rb:190:in `dispatch'
actionpack (5.0.6) lib/action_controller/metal.rb:262:in `dispatch'
actionpack (5.0.6) lib/action_dispatch/routing/route_set.rb:50:in `dispatch'
actionpack (5.0.6) lib/action_dispatch/routing/route_set.rb:32:in `serve'
actionpack (5.0.6) lib/action_dispatch/journey/router.rb:39:in `block in serve'
actionpack (5.0.6) lib/action_dispatch/journey/router.rb:26:in `each'
actionpack (5.0.6) lib/action_dispatch/journey/router.rb:26:in `serve'
actionpack (5.0.6) lib/action_dispatch/routing/route_set.rb:727:in `call'
newrelic_rpm (4.5.0.337) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
newrelic_rpm (4.5.0.337) lib/new_relic/rack/agent_hooks.rb:30:in `traced_call'
newrelic_rpm (4.5.0.337) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
newrelic_rpm (4.5.0.337) lib/new_relic/rack/browser_monitoring.rb:32:in `traced_call'
newrelic_rpm (4.5.0.337) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
warden (1.2.7) lib/warden/manager.rb:36:in `block in call'
warden (1.2.7) lib/warden/manager.rb:35:in `catch'
warden (1.2.7) lib/warden/manager.rb:35:in `call'
newrelic_rpm (4.5.0.337) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
rack (2.0.3) lib/rack/etag.rb:25:in `call'
newrelic_rpm (4.5.0.337) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
rack (2.0.3) lib/rack/conditional_get.rb:25:in `call'
newrelic_rpm (4.5.0.337) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
rack (2.0.3) lib/rack/head.rb:12:in `call'
newrelic_rpm (4.5.0.337) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
rack (2.0.3) lib/rack/session/abstract/id.rb:232:in `context'
rack (2.0.3) lib/rack/session/abstract/id.rb:226:in `call'
newrelic_rpm (4.5.0.337) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
actionpack (5.0.6) lib/action_dispatch/middleware/cookies.rb:613:in `call'
newrelic_rpm (4.5.0.337) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
activerecord (5.0.6) lib/active_record/migration.rb:553:in `call'
newrelic_rpm (4.5.0.337) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
actionpack (5.0.6) lib/action_dispatch/middleware/callbacks.rb:38:in `block in call'
activesupport (5.0.6) lib/active_support/callbacks.rb:97:in `__run_callbacks__'
activesupport (5.0.6) lib/active_support/callbacks.rb:750:in `_run_call_callbacks'
activesupport (5.0.6) lib/active_support/callbacks.rb:90:in `run_callbacks'
actionpack (5.0.6) lib/action_dispatch/middleware/callbacks.rb:36:in `call'
newrelic_rpm (4.5.0.337) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
actionpack (5.0.6) lib/action_dispatch/middleware/executor.rb:12:in `call'
newrelic_rpm (4.5.0.337) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
actionpack (5.0.6) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
newrelic_rpm (4.5.0.337) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
better_errors (2.4.0) lib/better_errors/middleware.rb:59:in `call'
newrelic_rpm (4.5.0.337) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
actionpack (5.0.6) lib/action_dispatch/middleware/debug_exceptions.rb:49:in `call'
newrelic_rpm (4.5.0.337) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
web-console (3.5.1) lib/web_console/middleware.rb:135:in `call_app'
web-console (3.5.1) lib/web_console/middleware.rb:20:in `block in call'
web-console (3.5.1) lib/web_console/middleware.rb:18:in `catch'
web-console (3.5.1) lib/web_console/middleware.rb:18:in `call'
newrelic_rpm (4.5.0.337) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
actionpack (5.0.6) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
newrelic_rpm (4.5.0.337) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
railties (5.0.6) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.0.6) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.0.6) lib/active_support/tagged_logging.rb:69:in `block in tagged'
activesupport (5.0.6) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.0.6) lib/active_support/tagged_logging.rb:69:in `tagged'
railties (5.0.6) lib/rails/rack/logger.rb:24:in `call'
newrelic_rpm (4.5.0.337) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
sprockets-rails (3.2.1) lib/sprockets/rails/quiet_assets.rb:13:in `call'
newrelic_rpm (4.5.0.337) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
actionpack (5.0.6) lib/action_dispatch/middleware/request_id.rb:24:in `call'
newrelic_rpm (4.5.0.337) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
rack (2.0.3) lib/rack/method_override.rb:22:in `call'
newrelic_rpm (4.5.0.337) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
rack (2.0.3) lib/rack/runtime.rb:22:in `call'
newrelic_rpm (4.5.0.337) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
activesupport (5.0.6) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
newrelic_rpm (4.5.0.337) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
actionpack (5.0.6) lib/action_dispatch/middleware/executor.rb:12:in `call'
newrelic_rpm (4.5.0.337) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
actionpack (5.0.6) lib/action_dispatch/middleware/static.rb:136:in `call'
newrelic_rpm (4.5.0.337) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
rack (2.0.3) lib/rack/sendfile.rb:111:in `call'
newrelic_rpm (4.5.0.337) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
railties (5.0.6) lib/rails/engine.rb:522:in `call'
newrelic_rpm (4.5.0.337) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
puma (3.10.0) lib/puma/configuration.rb:225:in `call'
puma (3.10.0) lib/puma/server.rb:605:in `handle_request'
puma (3.10.0) lib/puma/server.rb:437:in `process_client'
puma (3.10.0) lib/puma/server.rb:301:in `block in run'
puma (3.10.0) lib/puma/thread_pool.rb:120:in `block in spawn_thread'
  Rendering /usr/local/rvm/gems/ruby-2.3.4/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout
  Rendering /usr/local/rvm/gems/ruby-2.3.4/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
  Rendered /usr/local/rvm/gems/ruby-2.3.4/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (4.6ms)
  Rendering /usr/local/rvm/gems/ruby-2.3.4/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
  Rendered /usr/local/rvm/gems/ruby-2.3.4/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.2ms)
  Rendering /usr/local/rvm/gems/ruby-2.3.4/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
  Rendered /usr/local/rvm/gems/ruby-2.3.4/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.0ms)
  Rendered /usr/local/rvm/gems/ruby-2.3.4/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (35.2ms)

1 个答案:

答案 0 :(得分:1)

如果您follow the steps要将CancancanActiveAdmin进行整合,那么您只需根据需要使用您的用户角色扩展Ability#initialize

# app/models/ability.rb

def initialize(user)
  if user.admin?
    can :manage, :all
  elsif user.product_manager?
    can :manage, Product
    can :read, ActiveAdmin::Page, name: 'Dashboard'
  end
end

无需在每个控制器中调用load_and_authorize_resource