Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
865 views
in Technique[技术] by (71.8m points)

ruby on rails - Activeadmin formtastic dynamic select

I would like to make a dynamic select option via Activeadmin's formtastic like so:

  form do |f|
    f.inputs "Exam Registration Details" do
      f.input :user_id, :as => :select, :collection => User.where(:admin => 'false')
      #selects user from list. WORKING

      f.input :student_id, :as => :select, :collection => Student.joins(lessons: :user)
      #collection of students will change to students who have lessons with chosen user. NOT WORKING, returns all students who have lessons. 

      f.input :lesson_id, :as => :select, :collection => Lesson.joins(:student, :user)
      #collection of lessons will change to reflect lessons connected by chosen user and student. NOT WORKING, returns all lessons.
    end
    f.buttons
  end

My amateurish code is obviously not working as I intended it to. What changes should I make?

I have 4 models as below:

class Student < ActiveRecord::Base
  has_many :lessons
  has_many :users, through: :lessons
  has_many :exam_registrations, through: :lessons

class Lesson < ActiveRecord::Base
  belongs_to :user
  belongs_to :student
  belongs_to :exam_registration

class User < ActiveRecord::Base
  has_many :lessons
  has_many :students, through: :lessons
  has_many :exam_registrations, through: :lessons

class ExamRegistration < ActiveRecord::Base
  has_many :lessons
  has_many :users, through: :lessons
  has_many :students, through: :lessons
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

SOLVED

For anyone else wrestling with the same problem, look at this railscast

here's how I implemented multiple dynamic select menus in activeadmin:

config/initializers/active_admin.rb

  config.register_javascript 'exam_registrations.js.coffee'

app/admin/exam_registrations.rb

  form do |f|
    f.inputs "Exam Registration Details" do
      f.input :user_id, :label => 'Teacher', :as => :select, :collection => User.where(:admin => 'false', :active => true).order(:name), :include_blank => true
      f.input :student_id, :hint => 'Students grouped by teacher names', :as => :select, :collection => option_groups_from_collection_for_select(User.where(:admin => false, :active => true).order(:name), :students, :name, :id, :name)
      f.input :lesson_id, :hint => 'Lessons grouped by student names', :as => :select, :collection => option_groups_from_collection_for_select(Student.where(:active => true).order(:name), :lessons, :name, :id, :name)
    end
    f.buttons
  end

app/assets/javascripts/exam_registrations.js.coffee

#first menu    
jQuery ->
      $('#exam_registration_student_id').parent().hide()
      students = $('#exam_registration_student_id').html()
      $('#exam_registration_user_id').change ->
        user = $('#exam_registration_user_id :selected').text()
        escaped_user = user.replace(/([ #;&,.+*~':"!^$[]()=>|/@])/g, '\$1')
        options = $(students).filter("optgroup[label='#{escaped_user}']").html()
        if options
          $('#exam_registration_student_id').html(options)
          $('#exam_registration_student_id').parent().show()
        else
          $('#exam_registration_student_id').empty()
          $('#exam_registration_lesson_id').empty()

# second menu
  $('#exam_registration_lesson_id').parent().hide()
  lessons = $('#exam_registration_lesson_id').html()
  $('#exam_registration_student_id').click ->
    student = $('#exam_registration_student_id :selected').text()
    escaped_student = student.replace(/([ #;&,.+*~':"!^$[]()=>|/@])/g, '\$1')
    options = $(lessons).filter("optgroup[label='#{escaped_student}']").html()
    if options
      $('#exam_registration_lesson_id').html(options)
      $('#exam_registration_lesson_id').parent().show()
    else
      $('#exam_registration_lesson_id').empty()

restart the server and the menus work!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...