PostsController#index 中的 NameError

NameError in PostsController#index

我用过4个重要的gems

gem 'activeadmin'
gem 'devise'
gem 'cancancan', '~> 2.0' 
gem 'friendly_id', '~> 5.2.4'

我有 2 个主控制器 “用户”和“Post” 并在 active_admin(rails 中注册生成 active_admin:resource用户和rails生成active_admin:resourcePost)。我已经使用cancancan进行授权。
我在

中定义了能力

ability.rb

    if user.admin?  
        can :manage, :all
    else
        can :read, Posts
    end

ApplicationController.rb

class ApplicationController < ActionController::Base
    protect_from_forgery

  rescue_from CanCan::AccessDenied do |exception|
    respond_to do |format|
      format.json { head :forbidden, content_type: 'text/html' }
      format.html { redirect_to main_app.root_url, notice: exception.message }
      format.js   { head :forbidden, content_type: 'text/html' }
    end
  end
    def current_ability
      @current_ability ||= AccountAbility.new(current_admin_user)
    end
end

schema.rb

ActiveRecord::Schema.define(version: 2019_02_14_200911) do

  create_table "active_admin_comments", force: :cascade do |t|
    t.string "namespace"
    t.text "body"
    t.string "resource_type"
    t.integer "resource_id"
    t.string "author_type"
    t.integer "author_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["author_type", "author_id"], name: "index_active_admin_comments_on_author_type_and_author_id"
    t.index ["namespace"], name: "index_active_admin_comments_on_namespace"
    t.index ["resource_type", "resource_id"], name: "index_active_admin_comments_on_resource_type_and_resource_id"
  end

  create_table "admin_users", force: :cascade do |t|
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.boolean "admin", default: true
    t.index ["email"], name: "index_admin_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_admin_users_on_reset_password_token", unique: true
  end

  create_table "categories", force: :cascade do |t|
    t.string "title"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "posts", force: :cascade do |t|
    t.string "title"
    t.text "body"
    t.datetime "published_at"
    t.integer "user_id"
    t.integer "category_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "slug"
    t.index ["category_id"], name: "index_posts_on_category_id"
    t.index ["slug"], name: "index_posts_on_slug", unique: true
    t.index ["user_id"], name: "index_posts_on_user_id"
  end

  create_table "users", force: :cascade do |t|
    t.string "name"
    t.string "email"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "slug"
    t.boolean "admin", default: false
    t.index ["slug"], name: "index_users_on_slug", unique: true
  end

end

目标

我的目标是: 1.只有Admin可以post,添加用户,添加分类基本上都可以管理。
2. guest 用户只能读。 所以我用cancancangem做授权。但我收到错误:

uninitialized constant ApplicationController::AccountAbility
Extracted source (around line #12):
  end
    def current_ability
      @current_ability ||= AccountAbility.new(current_admin_user)
    end
end

如果您需要任何进一步的信息,我已经上传到 git
如果你能帮助我,那就太好了。提前致谢。

NameError 是因为您的模型被命名为 Ability,而不是 AccountAbility,因此请将您的 current_ability 方法更改为:

def current_ability
  @current_ability ||= Ability.new(current_admin_user)
end