Search code examples
ruby-on-railsdeviseactiveadminrolify

Filtering dropdown when creating a new item in Active Admin Rails


I have a Rails 5 Application that uses Devise for registrations and sessions via the standard User Model. I also have Rolify integrated with two types of roles (student,teacher).

class User < ApplicationRecord
  rolify
  mount_uploader :avatar, AvatarUploader

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  validates_integrity_of  :avatar
  validates_processing_of :avatar


  include PgSearch
  pg_search_scope :search_by_full_name, against: [:full_name],using: {
    tsearch: {
      prefix: true,
      highlight: {
        start_sel: '<b>',
        stop_sel: '</b>',
      }
    }
   }



  private
    def avatar_size_validation
      errors[:avatar] << "should be less than 500KB" if avatar.size > 0.5.megabytes
    end
end

I want to show some featured teachers, and so I have a table called featured_teachers. The migration is given below.

class CreateFeaturedTeachers < ActiveRecord::Migration[5.2]
  def change
    create_table :featured_teachers, id: :uuid do |t|
      t.references :user, foreign_key: true,type: :uuid

      t.timestamps
    end
  end
end

The associated FeaturedTeacher model is given below

class FeaturedTeacher < ApplicationRecord
  belongs_to :user
end

I want to use Active Admin to manage Featured Teachers and so I have created the following Active Admin resource.

ActiveAdmin.register FeaturedTeacher do

permit_params :user_id
actions :index,:new, :destroy
index do
    selectable_column
    column :user
    column :created_at
    actions name: "Actions"
end
end

But what is happening is, that when I want to add a new Featured Teacher, I am getting the complete list of Users in the database in the dropdown.

What I want to do is show only users with the role type 'teacher' and also users who have not been added to the Featured Teachers table already when adding new users to the Featured Teachers list in the Active Admin UI.

Can someone help me with this please? I am a bit new to Ruby and Rails and looking to understand how to get this done. I would most likely be needing this in other Active Admin interfaces as well.

Thank you.


Solution

  • You need to create form for featured teacher, please add below code in featured_teachers.rb active admin file

    form do |f|
        f.inputs 'Featured Teacher' do
          # in the select box only load those users which are teacher and not in featured teacher list you need to add query for it
          f.input :user, as: :select, collection: User.where(role: 'teacher').map { |u| [u.name, u.id] }, include_blank: true, allow_blank: false, input_html: { class: 'select2' }
         # please also add other fields of featured teacher model
    

    You can check all available options in active admin documentation here