简体   繁体   English

rails_admin 和 cancancan 未定义方法 `accessible_by' 产品:Class

[英]rails_admin and cancancan undefined method `accessible_by' for Product:Class

I am new to ruby-on-rails and i am looking for a way to have my rails_admin dashboard accessible only by superadmin users.我是 ruby-on-rails 的新手,我正在寻找一种方法让我的 rails_admin 仪表板只能由超级管理员用户访问。 I discovered that the rails_admin gem is fully compatible with another gem: cancancan, used for managing authorizations.我发现 rails_admin gem 与另一个 gem 完全兼容:cancancan,用于管理授权。

I followed this guide: https://github.com/sferik/rails_admin/wiki/Cancancan to configure rails_admin properly, and this is the result:我按照本指南: https://github.com/sferik/rails_admin/wiki/Cancancan正确配置 rails_admin,结果如下:

config/initializers/rails_admin.rb :配置/初始化程序/rails_admin.rb

RailsAdmin.config do |config|
  # == Devise ==
  config.authenticate_with do
    warden.authenticate! scope: :user
  end
  config.current_user_method(&:current_user)

  # == CancanCan ==
  config.authorize_with :cancancan

  config.actions do
    dashboard                     # mandatory
    index                         # mandatory
    new
    export
    bulk_delete
    show
    edit
    delete
    show_in_app
  end
end

app/models/ability.rb :应用程序/模型/能力.rb

class Ability
  include CanCan::Ability
  def initialize(user)
    user ||= User.new
    can :read, :all

    if user.superadmin_role?
      can :access, :rails_admin
      can :read, :dashboard
      can :manage, :all
    elsif user.admin_role? 
      can :manage, :all
    end
  end
end

When i created the project, i skipped active-records.当我创建项目时,我跳过了活动记录。 For now, the only scaffold i made was for a class called Product.目前,我制作的唯一支架是一个名为 Product 的 class。

I keep getting this error when navigating to localhost:3000/admin:导航到 localhost:3000/admin 时,我不断收到此错误:

NoMethodError in RailsAdmin::MainController#dashboard

undefined method `accessible_by' for Product:Class

I cannot figure out where and how to define this "accessible_by" method.我不知道在哪里以及如何定义这个“accessible_by”方法。 Moreover, i found that on this link https://github.com/CanCanCommunity/cancancan/wiki/Fetching-Records , is told that NOT using active records requires a model adapter.此外,我发现在此链接上https://github.com/CanCanCommunity/cancancan/wiki/Fetching-Records被告知不使用活动记录需要 model 适配器。

I'm using on MacOS 11.15.6 and:我在 MacOS 11.15.6 上使用并且:

  • Rails 6.0.2导轨 6.0.2
  • Rails_admin 2.0.2 Rails_admin 2.0.2
  • Cancancan 3.0.2康康康 3.0.2
  • Mongoid 7.0.5 Mongoid 7.0.5

Can someone help me understanding what am i doing wrong?有人可以帮我理解我做错了什么吗? Thanks:)谢谢:)

Ok, i got it!好,我知道了!

I solved the problem with these steps:我通过以下步骤解决了问题:

  • FIRST OF ALL: STOP rails server IF CURRENTLY RUNNING首先:如果当前正在运行,请停止rails server

  • Install the cancancan-mongoid gem.安装cancancan-mongoid gem。 Simply type gem 'cancancan-mongoid' in your Gemfile, UNDER the existing declaration of the cancancan gem.只需在您的 Gemfile 中输入gem 'cancancan-mongoid' ,在现有的 cancancan gem 声明下。 Then run bundle install .然后运行bundle install I referred to this page: https://github.com/CanCanCommunity/cancancan-mongoid我参考了这个页面: https://github.com/CanCanCommunity/cancancan-mongoid

  • Check that the order of the gems in your Gemfile looks like this:检查 Gemfile 中 gem 的顺序是否如下所示:

mongoid
cancancan
cancancan-mongoid
rails_admin

The first three steps should solve the problem, but if it persists, proceed with this last step:前三个步骤应该可以解决问题,但如果问题仍然存在,请继续执行最后一步:

This worked for me: :)这对我有用::)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM