PostsController #index中的NameError

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PostsController #index中的NameError相关的知识,希望对你有一定的参考价值。

我用了4个重要的宝石

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

我有2个主控制器“User”和“Post”并在active_admin中注册(rails generate active_admin:资源用户和rails生成active_admin:resource Post)。我已经使用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才能发帖,添加用户,添加类别基本管理全部。 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。 如果你可以帮助我会很棒。提前致谢。

答案

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

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

以上是关于PostsController #index中的NameError的主要内容,如果未能解决你的问题,请参考以下文章

Laravel5.1 搭建简单的社区--展示帖子页面

Rails RESTful URL:特定类别下的所有帖子

JS中删除数组中的元素方法

Laravel5.1 搭建简单的社区--发表帖子

[ jquery 过滤器 .eq( index | -index ) ] 此方法用于在选择器的基础之上精确筛选出第N个元素

编写程序,找出数组a中前n个元素中的最小元素及其下标