29. # app/models/ability.rb
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new
can :destroy, Post, user_id: user.id
end
end
# app/controllers/posts_controller.rb
class PostsController < ApplicationController
before_filter :authenticate_user!
load_and_authorize_resource
# app/views/posts/edit.erb
<% if can? :destroy, @user %>
<%= link_to '削除', posts, method: :delete %>
<% end %>