我一直在按照本指南使用和安装带有 Devise 和 CanCan 的 Rolify。但是,当我尝试使用在控制台中向用户添加角色时,出现user.add_role :admin
以下错误,指出users_roles
找不到表。
DEPRECATION WARNING: This dynamic method is deprecated. Please use e.g. Post.find_or_create_by(name: 'foo') instead. (called from irb_binding at (irb):7)
Role Load (0.2ms) SELECT "roles".* FROM "roles" WHERE "roles"."name" = 'admin' AND "roles"."resource_type" IS NULL AND "roles"."resource_id" IS NULL ORDER BY "roles"."id" ASC LIMIT 1
ActiveRecord::StatementInvalid: Could not find table 'roles_users'
这是我的文件:
用户.rb
class User < ActiveRecord::Base
rolify
has_and_belongs_to_many :roles
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :name, :email, :password, :password_confirmation, :remember_me
validates_presence_of :name
# attr_accessible :title, :body
end
能力.rb
class Ability
include CanCan::Ability
def initialize(user)
if user.has_role? :admin
can :manage, :all
else
can :read, :all
end
end
end
角色.rb
class Role < ActiveRecord::Base
has_and_belongs_to_many :users, :join_table => :users_roles
belongs_to :resource, :polymorphic => true
scopify
结尾
是的,我已经使用rake db:migrate
. 我正在使用 rails 4.0.2,那么我该从哪里开始呢?
谢谢!