Delete the delete link only on the show active page

I am working on an active admin gem. I just want to hide the delete link only from the show page . So I added the code below

ActiveAdmin.register ArticlesSkill do menu :parent => "ReadUp" actions :index, :show, :new, :create, :update, :edit index do column :name, sortable: :name column :description column "" do |resource| links = ''.html_safe links += link_to I18n.t('active_admin.view'), resource_path(resource), :class => "member_link edit_link" links += link_to I18n.t('active_admin.edit'), edit_resource_path(resource), :class => "member_link edit_link" if Article.where(articles_skill_id: resource.id).blank? links += link_to I18n.t('active_admin.delete'), resource_path(resource), :method => :delete, :confirm => I18n.t('active_admin.delete_confirmation'), :class => "member_link delete_link" else # links += link_to I18n.t('active_admin.delete'), "#", :confirm => ("This Skill has A related Article. You Can't Delete This Now"), :class => "member_link delete_link" links += link_to I18n.t('active_admin.delete'), resource_path(resource), :method => :delete, :confirm => I18n.t('active_admin.delete_confirmation'), :class => "member_link delete_link" end links end end end 

This is deleting the delete link on the show page, but on the index page, if I try to delete the record, it shows this error.

 The action 'destroy' could not be found for Admin::ArticlesSkillsController 

Can anyone help me with this? You are welcome.

+6
source share
5 answers

So, this is deprecated, but I needed to conditionally hide the edit / destroy buttons on both the index page and the display page, and although this helped me a lot, I felt that a more complete answer could help others faster.

Assume a go ...

Conditionally show links to the index

It's relatively simple, just do not include β€œactions” and instead include your own:

 index do id_column column :name column :foo column :bar column :funded # don't do this # actions # instead make our own column, with no name so it looks like AA column "" do |resource| links = ''.html_safe links += link_to I18n.t('active_admin.view'), resource_path(resource), class: "member_link show_link" if !resource.funded? links += link_to I18n.t('active_admin.edit'), edit_resource_path(resource), class: "member_link edit_link" links += link_to I18n.t('active_admin.delete'), resource_path(resource), method: :delete, confirm: I18n.t('active_admin.delete_confirmation'), class: "member_link delete_link" end links end end 

Conditional display of buttons when displaying

Here we need to remove all the default buttons from the display page, and then add the necessary buttons:

 # Remove the buttons from the show page config.action_items.delete_if { |item| item.display_on?(:show) } # Then add in our own conditional Edit Button # (note: 'loan' is the registered model name) action_item :edit, only: [ :show ] , if: proc { !loan.funded? } do link_to "#{I18n.t('active_admin.edit')} Loan", edit_resource_path(resource) end # and our Delete Button action_item :delete, only: [ :show ] , if: proc { !loan.funded? } do link_to "#{I18n.t('active_admin.delete')} Loan", resource_path(resource), method: :delete, confirm: I18n.t('active_admin.delete_confirmation') end # and our (custom) Fund Loan Button action_item :fund_loan, only: [ :show ], if: proc { !loan.funded? } do link_to 'Fund Loan', fund_loan_admin_loan_path(loan), method: :patch end # our custom actions code member_action :fund_loan, method: :patch do if resource.fund redirect_to resource_path(resource), notice: 'Loan funded' else redirect_to resource_path(resource), alert: "Loan funding failed : #{resource.errors.full_messages}" end end 

Hope this helps someone who stumbled upon this page. Happy coding =]

+3
source

Pass: also destroy the method invocation method or pass: all

 actions :all 
+2
source
 config.action_items.delete_if { |item| item.display_on?(:show) } action_item only: :show do link_to I18n.t('active_admin.edit'), edit_resource_path(resource) end 

Instead of actions, this code is used to remove the delete action link on the impression page.

+1
source

A simple way to remove an action from ActiveAdmin, such as delete in the controller

 ActiveAdmin.register User do actions :all, except: [:destroy] end 
0
source

I fixed my question. It was a simple thing.

Just override your controller.

 controller do def destroy super end end 
-1
source

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


All Articles