ActiveAdmin隐藏按条件删除操作

时间:2014-11-27 12:16:57

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

我有一些问题。

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

我是为#index页面做的。但我不知道如何使用#show页面进行这一操作。

此处的代码:

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

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

2 个答案:

答案 0 :(得分:6)

为此目的使用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

答案 1 :(得分:1)

这里的另一个解决方案 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