这可能是一个愚蠢的问题,但我离实现acl9已经很近了。只是有点不确定roles表中的数据应该是什么样子:
id | name | authorizable_type | authorizable_id | created_at | updated_at
---+-------+-------------------+-----------------+------------+------------ 我假设名字应该是"admin“之类的。
我不确定authorizable_type和authorizable_id指的是什么。
发布于 2012-12-20 11:10:28
authorizable_type和authorizable_id的主要用途是映射“`authorizable_object”。希望这通常会有所帮助,如果您在应用程序中创建一个角色表,您的迁移应该如下所示:
class CreateRoles < ActiveRecord::Migration
def self.up
create_table :roles, :force => true do |t|
t.string :name
t.string :authorizable_type
t.integer :authorizable_id
t.timestamps
end
add_index :roles, :name
add_index :roles, [:authorizable_id, authorizable_type]
add_index :roles, [:name, :authorizable_id, :authorizable_type], :unique => true
end
def self.down
remove_index :roles, [:name, :authorizable_id, :authorizable_type]
remove_index :roles, [:authorizable_id, :authorizable_type]
remove_index :roles, :name
drop_table :roles
end
end发布于 2015-01-23 04:17:55
首先,acl9 1.2版附带了一个生成器,用于迁移roles表(以及其他一些好东西),有关更多详细信息,请参阅bin/rails g acl9:setup -h。
其次,为了解释这些字段,Rails有一个称为多态关联(read more about it here)的特性,它允许您将一个模型与任何其他模型相关联。acl9使用此功能使您能够在您希望的任何其他模型上应用角色,在acl9术语中,我们将这些其他模型称为“对象”(read more about that here)。你所需要做的就是在你的模型中包含acts_as_authorization_object调用,这样你就可以这样做了:
user.has_role! :admin, school并且该用户对该school (且仅针对该特定学校)具有:admin角色。根据多态关联特性,authorizable_id将包含来自DB的该学校的主键,而authorizable_type将包含类名"School"。
https://stackoverflow.com/questions/13962896
复制相似问题