在rails网站上创建ruby并最近添加:
<% if (?can :manage, :table) %>
<%= link_to 'New Table', new_table_path %>
<% end %>
为了一些额外的安全性,现在它声明我不能这样做。我认为这可能与我的能力有关:
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new
can :read, :all
if user.role? "admin"
can :manage, :all
end
def initialize(user)
user ||= User.new
can :read, :all
if user.role? "coach"
can :manage, :all
end
def initialize(user)
user ||= User.new
can :read, :all
if user.role? "captain"
can :manage, :tournaments
can :manage, :results
end
def initialize(user)
user ||= User.new
can :read, :all
if user.role? "teammember"
can :manage, :individualresults
end
end
end
感谢您提供任何帮助。如果您需要其他代码,请告诉我们。
答案 0 :(得分:2)
Your ability file应该看起来像:
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new
can :read, :all
if (user.role? "admin" || user.role? "coach")
can :manage, :all
end
if user.role? "captain"
can :manage, Tournament
can :manage, Result
end
if user.role? "teammember"
can :manage, Individualresult
end
end
end
Checking a user action against the defined CanCan rules完成如下:
if can?(:create, Table)