このガイドに従って、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'
ここに私のファイルがあります:
user.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
role.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 を使用しています。
ありがとうございました!