Skip to content

Instantly share code, notes, and snippets.

@cjcolvar
Last active Dec 18, 2015
Embed
What would you like to do?
Batch delete example working with cancan's load_and_authorize_resource
class RolesController < ApplicationController
include Hydra::RoleManagement::RolesBehavior
prepend_before_filter :accept_batches, only: [:destroy]
def destroy
@roles.each {|role| role.destroy}
redirect_to roles_path, notice: "Successfully deleted groups: #{params[:ids].join(", ")}"
end
def accept_batches
if params[:id]
params[:ids] = [params[:id]].flatten
params[:id] = nil
end
params[:ids] ||= []
#This implementation of batch works based upon cancan's returning of all roles when no params[:id] is supplied
#Thus authorize_resource is checking can? :delete, Role instead of can? :delete, 1, can? :delete, 2, etc.
#Or is cancan able to check @roles instead?!?
@roles = Role.where(id: params[:ids])
end
end
resources :roles do
collection do
delete :destroy
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment