[ActiveAdmin] Conditionally show buttons on show page

For customize buttons in page show in ActiveAdmin, we use:

First, we remove the default buttons from the show page:

# Remove the buttons from the show page
config.action_items.delete_if { |item| item.display_on?(:show) }

Then, add Edit button with conditions

# Add Edit Button with conditional, in show page
action_item :edit,  only: [ :show ] do
  if condition # (Ex: product.is_published?)
    link_to "Edit Product", edit_product_path(resource)
  end
end

And, add Delete button

# and our Delete Button
action_item :delete,  only: [ :show ] do
  link_to "Delete", resource_path(resource), method: :delete, 
      data: { confirm: I18n.t('active_admin.delete_confirmation') }
end
Advertisements