ActiveAdmin Addons nested_select未显示下拉菜单,仅显示复选框求助
Hey there! Let's work through your ActiveAdmin_Addons issues step by step— I’ve tackled similar nested select and datepicker glitches before, so here’s how to get your attendance system up and running:
First: Fix the Nested Select Showing Checkboxes Instead of a Dropdown
The most common culprit here is accidentally adding multiple: true to your nested select input (that forces checkbox mode). Double-check your form code for that parameter and remove it if present. If that’s not the issue, ensure you’re on a stable version of ActiveAdmin_Addons by updating your Gemfile:
gem 'activeadmin_addons', '~> 1.10' # Use the latest stable release
Run bundle install and restart your server after updating.
Second: Set Up Cohort → Student Cascading Dropdowns
To make the student list update based on the selected cohort, you’ll need to leverage the depends_on option in nested_select, plus ensure your model associations are solid.
Step 1: Confirm Model Associations
Make sure your models are linked correctly:
# app/models/cohort.rb class Cohort < ApplicationRecord has_many :students end # app/models/student.rb class Student < ApplicationRecord belongs_to :cohort has_many :attendances end # app/models/attendance.rb class Attendance < ApplicationRecord belongs_to :student belongs_to :cohort # Or link via student if preferred— adjust your DB schema accordingly end
Step 2: Build the ActiveAdmin Form
Here’s the full form code to implement the cascading selects, datepicker, and attendance status dropdown:
# app/admin/attendances.rb ActiveAdmin.register Attendance do permit_params :class_date, :attendance_status, :student_id, :cohort_id form do |f| f.inputs 'Attendance Record' do # Datepicker setup (uses ActiveAdmin_Addons' enhanced picker) f.input :class_date, as: :datepicker, datepicker_options: { format: 'yyyy-mm-dd', autoclose: true, todayHighlight: true } # First dropdown: Select Cohort f.input :cohort_id, as: :nested_select, collection: Cohort.all.order(name: :asc), label_method: :name, # Replace with your Cohort's display field value_method: :id, prompt: 'Choose a Cohort' # Cascading dropdown: Students filtered by selected Cohort f.input :student_id, as: :nested_select, collection: Student.where(cohort_id: f.object.cohort_id).order(full_name: :asc), label_method: :full_name, # Replace with your Student's display field value_method: :id, prompt: 'Choose a Student', depends_on: :cohort_id # Critical: Links this dropdown to the cohort selection # Attendance status selector f.input :attendance_status, as: :select, collection: ['Present', 'Absent', 'Late'], prompt: 'Select Attendance Status' end f.actions end end
Third: Fix the Datepicker Not Working
Ensure you’ve properly registered ActiveAdmin_Addons’ assets in your initializer:
# config/initializers/active_admin.rb require 'active_admin_addons' ActiveAdmin.setup do |config| # ... Your existing config ... config.register_javascript 'active_admin_addons/all.js' config.register_stylesheet 'active_admin_addons/all.css' end
Restart your server after adding this, and clear your browser cache— sometimes old assets can block the datepicker from loading.
Bonus: For Large Datasets, Use AJAX Loading
If you have lots of cohorts/students, preloading all students upfront can slow things down. Switch to AJAX-based loading for the student dropdown:
- Update the student nested_select in your form:
f.input :student_id, as: :nested_select, ajax: { url: admin_students_path, data: { cohort_id: ':cohort_id' } }, label_method: :full_name, value_method: :id, prompt: 'Choose a Student', depends_on: :cohort_id
- Add a JSON response handler in your Student admin controller:
# app/admin/students.rb ActiveAdmin.register Student do # ... Your existing config ... controller do def index if params[:cohort_id] @students = Student.where(cohort_id: params[:cohort_id]).order(full_name: :asc) render json: @students.map { |s| { id: s.id, text: s.full_name } } else super end end end end
After implementing these changes, test the flow: select a cohort, verify the student dropdown updates, pick a status, and confirm the datepicker pops up correctly.
内容的提问来源于stack exchange,提问作者Hodari Toure'

