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

时间:2017-11-10 12:39:57

标签: ruby-on-rails views customization activeadmin

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

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

1 个答案:

答案 0 :(得分:3)

您需要使用方法,即在模型中称为虚拟属性:

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?