ruby-on-rails – Activeadmin formtastic动态选择

我想通过Activeadmin的formtastic制作动态选择选项,如下所示:
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,: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,: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

我的业余代码显然不能像我预期的那样工作.我应该做些什么改变?

我有4个型号如下:

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

class ExamRegistration < ActiveRecord::Base
  has_many :lessons
  has_many :users,through: :lessons
  has_many :students,through: :lessons

解决方法

解决了

对于其他遇到同样问题的人,请看这个railscast

这是我如何在activeadmin中实现多个动态选择菜单:

配置/初始化/ active_admin.rb

config.register_javascript 'exam_registrations.js.coffee'

应用程序/管理/ exam_registrations.rb

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

应用程序/资产/ JavaScript的/ 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(/([ #;&,'\\$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()

重启服务器,菜单工作!

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...