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

Refresh

April 2019

Views

298 time

1

in my rails application, i have installed active admin. in users index page, by default, all columns are getting displayed(User table columns). I want to add a custom column called "become user" in this users index view (which is not a column in User's table). under this column i want to display user name as a hyperlink. up on clicking of that link the admin will be logged in to that particular user account. in order to implement this switching feature, i am using switch user gem. how to customise this view in Active Admin? and how to generate a link for all users in 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 answers

3

You need to go with approach, that is called virtual attribute in your model:

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

Then add it to your ActiveAdmin setup

PS: check this for some additional details: Is there an easier way of creating/choosing related data with ActiveAdmin?