Rails 3 ActiveAdmin. Hiding buttons according to CanCan permissions

I have ActiveAdmin and CanCan working together. I have already set administrator and client rights.

Now I want to hide the buttons New, Edit, and Delete in accordance with the resolution set CanCan, but the next line gives me the error ...

config.clear_action_items! :if => proc{can? (:destroy, Shipment)} 

this, too,

 :if => proc{ can?(:destroy, Shipment)}, actions :all, :except => [:new, :create, :update, :edit, :destroy] 
+4
source share
1 answer

I use this monkey patch to check permissions before displaying buttons

 module ActiveAdmin class Resource module ActionItems # Adds the default action items to each resource def add_default_action_items # New Link on all actions except :new and :show add_action_item :except => [:new] do if controller.action_methods.include?('new') and can? :create, active_admin_config.resource_class link_to(I18n.t('active_admin.new_model', :model => ''), new_resource_path, :class => "new-link" ) end end # Edit link on show add_action_item :only => :show do if controller.action_methods.include?('edit') and can? :update, active_admin_config.resource_class link_to(I18n.t('active_admin.edit_model', :model => ''), edit_resource_path(resource), :class => "edit-link" ) end end # Destroy link on show add_action_item :only => :show do if controller.action_methods.include?("destroy") and can? :destroy, active_admin_config.resource_class link_to(I18n.t('active_admin.delete_model', :model => ''), resource_path(resource), :class => "delete-link" , :method => :delete, :data => {:confirm => I18n.t('active_admin.delete_confirmation')}) end end end end end end new and: show module ActiveAdmin class Resource module ActionItems # Adds the default action items to each resource def add_default_action_items # New Link on all actions except :new and :show add_action_item :except => [:new] do if controller.action_methods.include?('new') and can? :create, active_admin_config.resource_class link_to(I18n.t('active_admin.new_model', :model => ''), new_resource_path, :class => "new-link" ) end end # Edit link on show add_action_item :only => :show do if controller.action_methods.include?('edit') and can? :update, active_admin_config.resource_class link_to(I18n.t('active_admin.edit_model', :model => ''), edit_resource_path(resource), :class => "edit-link" ) end end # Destroy link on show add_action_item :only => :show do if controller.action_methods.include?("destroy") and can? :destroy, active_admin_config.resource_class link_to(I18n.t('active_admin.delete_model', :model => ''), resource_path(resource), :class => "delete-link" , :method => :delete, :data => {:confirm => I18n.t('active_admin.delete_confirmation')}) end end end end end end " module ActiveAdmin class Resource module ActionItems # Adds the default action items to each resource def add_default_action_items # New Link on all actions except :new and :show add_action_item :except => [:new] do if controller.action_methods.include?('new') and can? :create, active_admin_config.resource_class link_to(I18n.t('active_admin.new_model', :model => ''), new_resource_path, :class => "new-link" ) end end # Edit link on show add_action_item :only => :show do if controller.action_methods.include?('edit') and can? :update, active_admin_config.resource_class link_to(I18n.t('active_admin.edit_model', :model => ''), edit_resource_path(resource), :class => "edit-link" ) end end # Destroy link on show add_action_item :only => :show do if controller.action_methods.include?("destroy") and can? :destroy, active_admin_config.resource_class link_to(I18n.t('active_admin.delete_model', :model => ''), resource_path(resource), :class => "delete-link" , :method => :delete, :data => {:confirm => I18n.t('active_admin.delete_confirmation')}) end end end end end end data => {: confirm => I18n.t ( 'active_admin.delete_confirmation')}) module ActiveAdmin class Resource module ActionItems # Adds the default action items to each resource def add_default_action_items # New Link on all actions except :new and :show add_action_item :except => [:new] do if controller.action_methods.include?('new') and can? :create, active_admin_config.resource_class link_to(I18n.t('active_admin.new_model', :model => ''), new_resource_path, :class => "new-link" ) end end # Edit link on show add_action_item :only => :show do if controller.action_methods.include?('edit') and can? :update, active_admin_config.resource_class link_to(I18n.t('active_admin.edit_model', :model => ''), edit_resource_path(resource), :class => "edit-link" ) end end # Destroy link on show add_action_item :only => :show do if controller.action_methods.include?("destroy") and can? :destroy, active_admin_config.resource_class link_to(I18n.t('active_admin.delete_model', :model => ''), resource_path(resource), :class => "delete-link" , :method => :delete, :data => {:confirm => I18n.t('active_admin.delete_confirmation')}) end end end end end end 
0
source

Source: https://habr.com/ru/post/1391952/


All Articles