如何使用 ActiveAdmin 仅在显示视图中显示编辑 link

How to show edit link only in show view using ActiveAdmin

我想在 ActiveAdmin 的显示视图中显示 link。但它不应该出现在索引视图的网格中。我不知道该怎么做。我已经尝试了很多方法。

尝试 1

actions :all, except: proc{ params['id'].present? ? [:new, :destroy] : [:new, :destroy, :edit] }.call

失败 undefined local variable or method 'params'

尝试 2

  actions :all, only: :show, except: [:new, :destroy, :edit]
  action_item only: :show do
    link_to 'Edit Cash Out', "/admin/documents/#{params['id']}/edit" if params['id']
  end

这失败了,因为没有为编辑路径创建路由

尝试 3

  actions :all, except: [:new, :destroy].tap do |a|
    a << :edit unless params['id'].present?
  end

失败 no block given

你能试试下面的代码吗?

actions :all, only: :show, except: [:new, :destroy, :edit]

action_item :view, only: :show do
  link_to 'Edit Cash Out', "/admin/documents/#{params['id']}/edit" if params['id']
end

如果我没记错的话,你只想隐藏link到索引页(在网格中)的编辑操作。

如果这是正确的,则此答案中包含一种可能的解决方案:

possible solution

index do
  column :actions do |item|
    links = []
    links << link_to('Show', show_item_path)
    links << link_to('Delete', delete_item_path, method: :delete, confirm: 'Are you sure?')
    links.join(' ').html_safe
  end
end

请记住调整上述代码以适应路由和命名。

希望这对你有用。