Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove delete link only in activeadmin show page

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.

like image 264
Karthikds Avatar asked May 14 '13 09:05

Karthikds


3 Answers

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...

Conditionally Show Links on Index

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

Conditionally Show Buttons on Show

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 =]

like image 172
Ryan Crews Avatar answered Sep 25 '22 02:09

Ryan Crews


Simple way to remove action from ActiveAdmin like delete in controller

ActiveAdmin.register User do

     actions :all, except: [:destroy]

end 
like image 26
Nikhil Thombare Avatar answered Sep 22 '22 02:09

Nikhil Thombare


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.

like image 26
Balachandran Avatar answered Sep 25 '22 02:09

Balachandran