50

current_user次の方法を使用して、ビューまたはコントローラーから のパーミッションを取得できますcan?

  <% if can? :update, @article %>
    <%= link_to "Edit", edit_article_path(@article) %>
  <% end %>

この構文を使用してモデルからこの機能にアクセスするにはどうすればよいですか:

user.can?(:update, @article)
4

1 に答える 1

74

これについては、github に wiki エントリがあります: https://github.com/ryanb/cancan/wiki/ability-for-other-users

User モデルを次のように変更する必要があります。

class User < ActiveRecord::Base
  def ability
    @ability ||= Ability.new(self)
  end
  delegate :can?, :cannot?, :to => :ability
end

次に、次のような能力を確認できます。

user.can?(:update,@article)
于 2010-07-20T19:19:10.073 に答える