0

Ruby on Rails サイトを作成し、最近追加したもの:

<% 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

助けてくれてありがとう。追加のコードが必要な場合はお知らせください。

4

1 に答える 1

2

アビリティファイルはおそらく次のようになります。

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

定義された CanCan ルールに対するユーザー アクションのチェックは、次のように行われます。

if can?(:create, Table)
于 2012-05-08T04:22:48.927 に答える