【问题标题】:How to add a custom column which is not present in table in active admin in rails?如何在rails的活动管理员中添加表中不存在的自定义列?
【发布时间】:2017-11-10 12:39:57
【问题描述】:

在我的 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

【问题讨论】:

标签: ruby-on-rails views customization activeadmin


【解决方案1】:

您需要采用方法,即模型中的虚拟属性:

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?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-16
    • 2016-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多