You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rails 6中ActiveAdmin如何移除默认的「新建个人账户」按钮?

解决ActiveAdmin移除「New Individual Accounts」默认按钮的问题

问题原因

你之前写的action_item代码是新增了一个按钮,而非覆盖默认按钮——ActiveAdmin中action_item默认是追加动作按钮,不是替换原有按钮。

解决方案

根据需求,有两种常用处理方式:

方式1:完全移除该资源的「新建」相关入口(含菜单+页面按钮)

如果不需要任何新建该资源的入口,直接在资源注册块中指定允许的动作,排除:new:

ActiveAdmin.register Account, as: "Individual Accounts" do
  menu parent: "Accounts", label: "Individual Accounts"
  # 只保留需要的动作,去掉新建动作
  actions :index, :show, :edit, :destroy
  permit_params :first_name, :last_name, :email, :password, :role_id

  # 其余原有代码保持不变...
end

方式2:仅移除页面上的「New Individual Accounts」按钮(保留菜单入口)

如果只是不想在列表页显示新建按钮,但保留菜单里的新建选项,可以用remove_action_item精准移除默认按钮:

ActiveAdmin.register Account, as: "Individual Accounts" do
  menu parent: "Accounts", label: "Individual Accounts"
  permit_params :first_name, :last_name, :email, :password, :role_id

  # 移除index页面的默认新建按钮
  remove_action_item :new, only: :index

  # 其余原有代码保持不变...
end

补充说明

  • 也可以用actions :all, except: [:new]替代方式1的动作声明,效果一致,适合需要保留大部分默认动作的场景
  • 后续如果需要自定义其他动作按钮,直接用action_item追加即可,不会和移除操作冲突

内容的提问来源于stack exchange,提问作者Shruthi R

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 18:15:40