【问题标题】:ActiveAdmin hide Delete action by conditionActiveAdmin 按条件隐藏删除操作
【发布时间】:2014-11-27 12:16:57
【问题描述】:

我有一些问题。

在 ActiveAdmin 中,我需要按条件隐藏 DELETE 操作。

我是为#index 页面做的。但我不知道如何使用#show page 来实现这个技巧。

代码如下:

index do
    selectable_column
    column :id do |package|
      link_to package.id, admin_subscription_package_path(package)
    end
    column :title
    column :plan_status
    column :duration do |package|
      if package.duration == 1
        "#{package.duration} day"
      else
        "#{package.duration} days"
      end
    end
    column 'Price (USD)', :price do |package|
      number_to_currency(package.price, locale: :en)
    end
    column :actions do |object|
      raw(
          %(
            #{link_to 'View', admin_subscription_package_path(object)}
            #{(link_to 'Delete', admin_subscription_package_path(object),
                       method: :delete) unless object.active_subscription? }
            #{link_to 'Edit', edit_admin_subscription_package_path(object)}
          )
      )

    end
  end

或者也许我可以一次对所有页面更有用。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 activeadmin


    【解决方案1】:

    为此目的使用action_item:

    ActiveAdmin.register MyModel
    
      actions :index, :show, :new, :create, :edit, :update, :destroy
    
      action_item only: :show  do
        if condition
          link_to "Delete whatever", {action: :destroy}, method: :delete, confirm: 'Something will be deleted forever. Sure?'
        end
      end
    
    end
    

    【讨论】:

    • 谢谢,但在这种情况下如何获取当前对象?像这样:除非 object.active_subscription?
    • 使用 resource.active_subscription。在activeadmin资源是current_object,你可以通过将raise resource.inspect放在action_item里面来检查它
    • 不错。 Thx,但我有一些问题。默认操作显示为。我试过做这个 config.clear_action_items!但是在这种情况下,所有页面的操作都被删除了,但我只需要显示。
    • 添加操作 :index, :show, :new, :create 等来选择要显示的操作(但仍然保留 config.clear_action_items!)
    • 它应该在哪里以及如何?
    【解决方案2】:

    这里的另一个解决方案 https://groups.google.com/g/activeadmin/c/102jXVwtgcU

    ActiveAdmin.register Foo do
    
      RESTRICTED_ACTIONS = ["edit", "update"]
      actions [:index, :show, :edit, :update]
    
      controller do
        def action_methods
          if current_admin_user.role?(AdminUser::ADMIN_ROLE)
            super
          else
            super - RESTRICTED_ACTIONS
          end
        end
      end
    
      ...
    end
    

    【讨论】:

      【解决方案3】:

      我一直在寻找一种方法来解决这个问题,而无需自己手写按钮/操作链接。

      在阅读了一些活跃的管理代码之后,我发现了这个 hack:

      
      ActiveAdmin.register User do # replace User by the type of the resource in your list
      
        # ... your config, index column definitions, etc.
      
        controller do
      
          # ... maybe some other controller stuff
      
          def authorized?(action, resource) 
            return false unless super(action, resource)
      
            if resource.is_a? User # replace User by the type of the resource in your list
              return false if action == ActiveAdmin::Auth::DESTROY && condition  # replace condition with your check involving the resource
            end
      
            true
          end
      
        end
      end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-27
        • 2015-01-24
        相关资源
        最近更新 更多