Reputation: 247
I am working on the active admin gem. I just want to hide the delete link only from show page. So, I added the below code
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 removing the delete link from the show page, But in the index page if I try to delete a record, its showing this error,
The action 'destroy' could not be found for Admin::ArticlesSkillsController
Any one can help me in this? Please.
Upvotes: 8
Views: 5895
Reputation: 593
Small adjustment to the Ryan answer, for those who do not like using direct strings manipulations:
actions(defaults: false) do |resource|
item I18n.t('active_admin.view'), resource_path(resource), class: "member_link show_link"
next if resource.funded?
item I18n.t('active_admin.edit'), edit_resource_path(resource), class: "member_link edit_link"
item I18n.t('active_admin.delete'), resource_path(resource),
method: :delete,
confirm: I18n.t('active_admin.delete_confirmation'),
class: "member_link delete_link"
end
Upvotes: 0
Reputation: 3033
So this is old, but I found myself needing to conditionally hide the edit/destroy buttons on both the index page and the show page, and while this helped me a lot, I felt a more complete answer might help others quicker.
Let's give that a go...
This one is relatively straightforward, simply don't include the "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
Here we'll have to remove all the default buttons from the show page and then add in the buttons we want:
# 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's 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
Hopefully that helps out someone who stumbles upon this page. Happy coding =]
Upvotes: 9
Reputation: 160
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 action delete link from show page.
Upvotes: 3
Reputation: 1198
Simple way to remove action from ActiveAdmin like delete in controller
ActiveAdmin.register User do
actions :all, except: [:destroy]
end
Upvotes: 3
Reputation: 247
I have fixed my Issue. It was a simple thing.
Just override your controller.
controller do
def destroy
super
end
end
Upvotes: -4
Reputation:
Pass :destroy also to actions method call, or pass :all
actions :all
Upvotes: 1