Access to CanCan `can?` Method From Model

Can you get current_user permissions from a view or controller using can? in the following way:

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

How can I access this function from the model using this syntax:

 user.can?(:update, @article) 
+46
ruby-on-rails cancan
Jul 20 '10 at 19:06
source share
1 answer

To do this, there is a wiki entry on github: https://github.com/ryanb/cancan/wiki/ability-for-other-users

You need to change your user model as follows:

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

Then you can test such abilities:

 user.can?(:update,@article) 
+68
Jul 20 '10 at 7:19
source share



All Articles