如何在 rails 的活动管理员中添加 table 中不存在的自定义列?

How to add a custom column which is not present in table in active admin in rails?

在我的 rails 应用程序中,我安装了 active admin。在 users index 页面中,默认显示 all columns(用户 table 列)。我想添加一个 custom column called "become user" in this users index view(这不是用户 table 中的一列)。在此 column 下,我想将 user name 显示为 hyperlink。单击 link 后,admin 将登录到该特定用户帐户。为了实现这个切换功能,我使用 switch user gem。如何在 Active Admin 中自定义此视图?以及如何为活动管理员

中的所有用户生成 link
ActiveAdmin.register User do

  permit_params do
    permitted = [:email, :fname, :lname, :phone]
    #   permitted << :other if resource.something?
    #   permitted
end

  # Filterable attributes on the index screen
  filter :email
  filter :fname
  filter :lname
  filter :phone
  filter :current_sign_in_at
  filter :created_at


  # Customize columns displayed on the index screen in the table
  index do
    selectable_column
    id_column
    column :email
    column :fname
    column :lname
    column :phone
    column :current_sign_in_at
    # column :sign_in_count
    column :created_at

    # actions
  end

  form do |f|
    inputs 'Details' do
      input :email
      input :password
      input :fname
      input :lname
      input :phone
    end
    actions
  end

  controller do
  end

end

您需要采用在您的模型中称为虚拟属性的方法:

class User < ApplicationRecord
  def become_user
    "I am a virtual attribute of this user"
  end
end

然后将其添加到您的 ActiveAdmin 设置中

PS:检查此以获取更多详细信息:Is there an easier way of creating/choosing related data with ActiveAdmin?