ruby-on-railsrubyactiverecordactiveadminformtastic

How to resolve associated models dropdown menu prob in active admin


I m getting unreadable values @ dropdown menu in active admin. I have an attribute which has a inclusion of specific values(around 10 values) but when I am making a new object of that class by using active admin... firstly it is showing the unreadable drop down menu enter image description here second its showing that attribute to be blank even if I m choosing some unreadable value.. enter image description here plz help

my admin/Resident.rb page: :

ActiveAdmin.register Resident do
   permit_params :room_number,:roll_number,:name,:hostel,:hostel_id
   index do
    column :room_number
    column :roll_number
    column :name
    column :hostel
    actions
  end
  filter :name,:as => :string
  filter :hostel, :as => :select
  filter :room_number
  filter :roll_number

   form do |f|
     f.semantic_errors *f.object.errors.keys
     inputs 'Enter the student details' do
       input :room_number
       input :roll_number
       input :name
       input :hostel
       actions
     end
   end
end

I have two models : Hostel And Resident : models/hostel.rb

class Hostel < ActiveRecord::Base
  has_many :residents
end

models/resident.rb

class Resident < ActiveRecord::Base

  belongs_to  :hostel
  validates :room_number,presence: true,uniqueness: {case_sensitive: false}
  validates :roll_number,presence: true,uniqueness:{case_sensitive: false}
  validates :name, presence: true,length:{ maximum: 50 }
  validates :hostel,presence: true

  def display_name
    hostel
  end

end

schema: : Resident

create_table "residents", force: :cascade do |t|
    t.string   "room_number"
    t.string   "roll_number"
    t.string   "name"
    t.string   "hostel"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
    t.integer  "hostel_id"
  end

Hostel:

create_table "hostels", force: :cascade do |t|
    t.string   "hostel"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false

end


Solution

  • For making the text readable, you can reimplement the "to_s" method :

    class Hostel < ActiveRecord::Base
      #... 
      def to_s
         self.name
      end
    end
    

    But active admin is smart enough to use the "name" row if existing usually. The bad side of this method is everywhere in your log etc. it will use the name. Something like [self.id,self.name].join('-') seems better for debugging if the name field of your table hostels is not unique.

    Your second problem is caused by one of theses two things:

    1. Check your params permits, and allow "hostel_id"

    2. Ensure your <select name="model[hostel_id]" > and not <select name="model[hostel]">. This should be done automatically, if not it's probably because you didn't defined well the link has_many / belongs_to in your two models.

    Finally, the last way is you can still push your own collection as parameter of the f.input into the active admin:

      f.input :hostel_id, collection: Hostel.all.map{|x| [x.name, x.id]}
    

    One more time, this should be done automatically. In your case it's not, so look closely your db schema.

    Happy Coding,

    Yacine.