[英]Ruby on Rails, can't get cancan to work
I have been trying to get cancan to work all day. 我一直在努力让康康整天工作。 I have started over several times using different tutorials but I keep getting the same errors.
我已经开始使用不同的教程多次,但是我仍然遇到相同的错误。
It's pretty simple, I have User account (created with Devise) which can have one role, either Admin or User. 很简单,我有一个用户帐户(使用Devise创建),该帐户可以具有一个角色,即Admin或User。 Here is the ability class:
这是能力等级:
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new
if user.role? :admin
can :manage, :all
end
end
end
In the profile controller I have the line load_and_authorize_resource
, the class User contains ROLES = %w[admin user]
. 在概要文件控制器中,我有一行
load_and_authorize_resource
,类User包含ROLES = %w[admin user]
。 Going to http://localhost:3000/profiles/
gives me the error 去
http://localhost:3000/profiles/
给我错误
wrong number of arguments (2 for 1)
in app/models/ability.rb:6:in
initialize'` app/models/ability.rb:6:in
initialize'中wrong number of arguments (2 for 1)
Using the alternative method user.admin?
使用替代方法
user.admin?
gives 给
undefined method `admin?' for #<User:0x5b34c10>
Googling the error above gives many results so I'm not the only person having this problem, but none of the solutions have worked for me. 谷歌搜索上面的错误给出了很多结果,所以我不是唯一遇到此问题的人,但是没有一个解决方案对我有用。
And yes I have added the role column to the User table 是的,我已经将角色列添加到用户表中
class AddRoleToUsers < ActiveRecord::Migration
def change
add_column :users, :role, :string
end
end
added the Gem, ran bundle install and restarted the server. 添加Gem,运行捆绑安装并重新启动服务器。
Providing you have the following this should work: 如果您具有以下条件,则该方法应该起作用:
Ability.rb 能力
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user
# raise user.role?(:administrator).inspect
if user.role? :administrator
can :manage, :all
can :manage, User
elsif user.role? :user
can :read, :all
end
end
end
RoleUser.rb RoleUser.rb
class RoleUser < ActiveRecord::Base
# attr_accessible :title, :body
belongs_to :user
belongs_to :role
end
Role 角色
class Role < ActiveRecord::Base
attr_accessible :name
has_and_belongs_to_many :users
end
User.rb User.rb
class User < ActiveRecord::Base
has_and_belongs_to_many :roles
def role?(role)
self.roles.find_by_name(role.to_s.camelize)
end
You need this as you are defining your roles, finding them, converting the role to a string and camelizing it. 在定义角色,查找角色,将角色转换为字符串并将其驼峰化时,需要使用此功能。
seeds.rb seed.rb
%w(Employee Administrator).each { |role| Role.create!(:name => role)}
creates an array User
and Administrator
. 创建一个数组
User
和Administrator
。
Should you follow these steps correctly you this should work. 如果您正确地遵循了这些步骤,就可以正常工作。 And also ensure you have the following migrations:
并确保您具有以下迁移:
class UsersHaveAndBelongsToManyRoles < ActiveRecord::Migration
def self.up
create_table :roles_users, :id => false do |t|
t.references :role, :user
end
end
def self.down
drop_table :roles_users
end
end
Then in your view you should use the can?
那么您认为应该使用
can?
by doing something like 通过做类似的事情
<% if can? :manage, User %>
.......
....
...
<% end %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.