ruby-on-rails 删除删除链接只在activeadmin显示页

mccptt67  于 2023-05-02  发布在  Ruby
关注(0)|答案(6)|浏览(160)

我正在使用active 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

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

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

有人能帮我吗?拜托了

vs3odd8k

vs3odd8k1#

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

索引有条件显示链接

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

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

Show时有条件显示按钮

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

# 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

希望这能帮助那些偶然发现这一页的人。Happy coding =]

zu0ti5jz

zu0ti5jz2#

从ActiveAdmin中删除操作的简单方法,如在控制器中删除

ActiveAdmin.register User do

     actions :all, except: [:destroy]

end
nnvyjq4y

nnvyjq4y3#

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

此代码用于从显示页面中删除操作删除链接,而不是操作。

0wi1tuuw

0wi1tuuw4#

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

actions :all
wpcxdonn

wpcxdonn5#

对Ryan的回答做了一个小的调整,对于那些不喜欢使用直接字符串操作的人:

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
0vvn1miw

0vvn1miw6#

我已经解决了我的问题。这是一件很简单的事情。
只要覆盖你的控制器。

controller do    
  def destroy
    super
  end 
end

相关问题