仅在activeadmin显示页面中删除删除链接

Kar*_*kds 6 ruby-on-rails activeadmin

我正在处理活动的admin gem.我只想隐藏显示页面中的删除链接.所以,我添加了以下代码

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
Run Code Online (Sandbox Code Playgroud)

这是从显示页面删除删除链接,但在索引页面,如果我尝试删除记录,它显示此错误,

The action 'destroy' could not be found for Admin::ArticlesSkillsController
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮助我吗?请.

Rya*_*ews 8

所以这是旧的,但我发现自己需要有条件地隐藏索引页面和显示页面上的编辑/销毁按钮,虽然这对我有很大帮助,但我觉得更完整的答案可能会更快地帮助其他人。

让我们试一试...

有条件地在索引上显示链接

这个相对简单,只是不包括“动作”,而是包括你自己的:

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
Run Code Online (Sandbox Code Playgroud)

有条件地在节目中显示按钮

在这里,我们必须从显示页面中删除所有默认按钮,然后添加我们想要的按钮:

# 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
Run Code Online (Sandbox Code Playgroud)

希望这对偶然发现此页面的人有所帮助。快乐编码 =]


小智 1

将 :destroy 也传递给 actions 方法调用,或传递 :all

actions :all
Run Code Online (Sandbox Code Playgroud)