Question

A simple question about ActiveAdmin / Formtastic. ActiveAdmin uses formtastic for it's forms.

I registered a model called ClassSection in ActiveAdmin. Here is app/admin/class_section.rb

ActiveAdmin.register ClassSection do

  permit_params :max_students, :min_students, :info, :class_course_id, :location_id

  form do |f|
    f.inputs "Details" do
      f.input :class_course
      f.input :min_students, label: "Minunum Students"
      f.input :max_students, label: "Maxiumum Students"
      f.input :location
    end
    f.inputs do
      f.input :info, label: "Additional Information"
    end
    f.actions
  end

end

Notice the line in the form for f.input :class_course. The class course has a couple of members: :name and :id_num. Name is the title of the course (for example 'Introduction to Poetry'). And :id_num is course number (for example 'ENG 101'). The current behavior is a drop down menu with the names from the class_course listed. Instead, I'd like the :id_num be the thing that the drop down is associated to. Is there an option I can set on that line to change it?

Here are the models:

class ClassSection < ActiveRecord::Base
  belongs_to :class_course
  has_many :class_dates
  belongs_to :location
end

and

class ClassCourse < ActiveRecord::Base
  has_many :class_sections
end

Also the schema for those two models:

create_table "class_courses", force: true do |t|
  t.string   "id_num"
  t.string   "name"
  t.text     "description"
  t.datetime "created_at"
  t.datetime "updated_at"
end

add_index "class_courses", ["id_num"], name: "index_class_courses_on_id_num"

create_table "class_sections", force: true do |t|
  t.integer  "class_course_id"
  t.integer  "min_students"
  t.integer  "max_students"
  t.integer  "location_id"
  t.text     "info"
  t.datetime "created_at"
  t.datetime "updated_at"
end
Was it helpful?

Solution

You can use the member_label option to the f.input in Formtastic for collections to have it use a different method on the target model to generate the drop down labels:

f.input :class_course, member_label: :id_num
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top