title: Understanding Cancan abilities
body: <p>Trying to get Cancan securing a few models in an application and curious why it's not working the way I thought it would. I had thought you could <code>can?</code> on the specific instance as opposed to the entire class so, not in this example but, you could enable abilities on a per instance basis as a list of posts are displayed?!?</p>
  
  <pre><code>class Ability
    include CanCan::Ability
    def initialize(user) 
      user ||= User.new # guest user (not logged in)
      if user.role? :admin
        can :manage, :all
      elsif user.role? :moderator
        can :manage, Post
      else
        can :read, :all
      end
    end
  end
  
  # posts/index.html.haml
  ...
  - if can? :update, @post   &lt;- doesn't work
  - if can? :update, Post    &lt;- works
  </code></pre>
  
  <p><strong>Edit</strong>: add PostsController.rb</p>
  
  <pre><code>#posts_controller.rb
  class PostsController &lt; ApplicationController
    before_filter :login_required, :except =&gt; [:index, :show]
    load_and_authorize_resource :except =&gt; [:create]
  
    def index
      # @posts = Post.all ## &lt;- handled by Cancan's load_and_authorize_resource
      @events = Event.where("end_date &lt;= :today", :today =&gt; Date.today)
      @next_event = Event.next
  
      respond_to do |format|
        format.html # index.html.erb
        format.json { render json: @posts }
      end
    end
    ...
  end
  </code></pre>