Rails 3 ActiveAdmin。根据 CanCan 权限隐藏按钮

发布于 2024-12-27 17:58:39 字数 356 浏览 0 评论 0原文

我让 ActiveAdmin 和 CanCan 一起工作。我已经设置了管理员和客户权限。

现在我想根据 CanCan 设置的权限隐藏“新建”、“编辑”和“删除”按钮,但以下行给了我错误...

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

这也是

:if => proc{ can?(:destroy, Shipment)}, actions :all, :except => [:new, :create, :update, :edit, :destroy]

I have ActiveAdmin and CanCan working together. I already set the administrator and customer permissions.

Now I want to hide the New, Edit and Delete buttons according to the permissions set by CanCan but the following line gives me errors...

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

This one too

:if => proc{ can?(:destroy, Shipment)}, actions :all, :except => [:new, :create, :update, :edit, :destroy]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你好,陌生人 2025-01-03 17:58:39

我使用这个猴子补丁在显示按钮之前检查权限

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文