NameError в PostsController # index - PullRequest
       21

NameError в PostsController # index

0 голосов
/ 15 февраля 2019

Я использовал 4 важных драгоценных камня

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

У меня есть 2 главных контроллера "Пользователь" и "Публикация" и я зарегистрирован в active_admin (rails generate active_admin: ресурс User и rails generateactive_admin: ресурс Post). Я использовал cancancan для авторизации.
Я определил способность в

способность. 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. Публиковать сообщения, добавлять пользователей, добавлять категории в основном все могут только администраторы.
2. Гость Пользователь может только читать.Поэтому я использовал cancancan gem для авторизации.Но я получаю сообщение об ошибке:

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

И я загрузил на git , если вам нужна дополнительная информация.
Будет здорово, если вы сможете мне помочь.Заранее спасибо.

1 Ответ

0 голосов
/ 15 февраля 2019

Ошибка NameEr: ваша модель называется Ability, а не AccountAbility, поэтому измените метод current_ability на:

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