Skip to content

Cancan and existing resource action items (buttons)

houen edited this page Jan 26, 2012 · 2 revisions

In order to user CanCan in the scope of a custom action, you need to instantiate the Ability class. Here is an example where a Activate / Deactivate button is shown to the user. (See also Add custom actions and buttons)


action_item :only => :show do
  user = User.find(params[:id])
  ability = Ability.new(current_user)
  if user.active
    link_to("Deactivate", deactivate_admin_user_path(user), :method => :put) if ability.can? :deactivate, User
  else
    link_to("Activate", activate_admin_user_path(user), :method => :put) if ability.can? :activate, User
  end
end