Невозможно настроить cancancan в Rails - PullRequest
0 голосов
/ 21 февраля 2019

Я использую active_admin и cancancan gem.

able.rb

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

У меня есть role столбец в базе данных.Это в основном блог.

schema.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"
    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

user.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

Цель

Моя цель - не допуститьпользователь-гость для создания нового сообщения.

Проблема

Проблема: мне отказано в доступе как для администратора, так и для гостя, хотя я четко определил, что различные типы пользователей могут делать в skill.rb .

Если вам нужна дополнительная информация о коде, вы можете проверить его на github .

Заранее спасибо.

1 Ответ

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

Проблема в том, что вы переопределяете user, который передается в класс Способностей.Удалить эту строку:

user = User.new(role: "guest")
...