gpt4 book ai didi

ruby-on-rails - 仅在 activeadmin 显示页面中删除删除链接

转载 作者:行者123 更新时间:2023-12-04 14:29:48 27 4
gpt4 key购买 nike

我正在研究事件管理 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

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

最佳答案

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

让我们试一试...

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

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

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

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

在这里,我们必须从显示页面中删除所有默认按钮,然后添加我们想要的按钮:
# 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

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

关于ruby-on-rails - 仅在 activeadmin 显示页面中删除删除链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16539998/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com