无法在Rails中使用活动的\u admin配置cancancan

时间 2019-02-21
阅读 40
点赞 0
收藏 0
连接pushpam-kumar

我正在尝试建立一个博客网站。我正在使用active_admincancancan宝石。我做的两个控制器是PostCategory.

Posts_控制器.rb

class PostsController < InheritedResources::Base
    load_and_authorize_resource

  private

    def post_params
      params.require(:post).permit(:title, :body, :user_id, :published_at)
    end
end

类别_controller.rb

class CategoriesController < InheritedResources::Base
    load_and_authorize_resource
  private

    def category_params
      params.require(:category).permit(:category)
    end
end

用户.rb

class User < ApplicationRecord
    has_many :posts
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, 
         :recoverable, :rememberable, :validatable
  def admin?
    role == "admin"
  end
  def regular?
    role == "regular"
  end
  def guest?
    role == "guest"
  end
end

能力.rb

class Ability
  include CanCan::Ability

  def initialize(user)
    # Define abilities for the passed in user here. For example:
    #
    #user = User.new()
    if user.admin?
        can :manage, :all
    else 
        can :read, :all 
    end
  end
end

架构.rb

ActiveRecord::Schema.define(version: 2019_02_18_221247) 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 "categories", force: :cascade do |t|
    t.string "category"
    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.integer "user_id"
    t.date "published_at"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "category_id"
  end

  create_table "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.string "role", default: "guest"
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end

end

我正在尝试根据定义授权role,角色列在用户表中,默认情况下,新用户是来宾用户。

我的目标是允许来宾用户只阅读文章。

问题:尽管我已经清楚地定义了不同类型的用户可以在其中执行什么操作,但是管理员和来宾用户的访问都被拒绝。能力.rb.

如果你需要更多关于代码的信息,你可以查看它。github.

建议答案
👍 0