2013-07-03 6 views
8

を選択します。私は何を変えるべきですか?は私がformtasticそうのような<em>Activeadmin</em>さん経由で動的な選択オプションをしたいと思い

は、私は以下のように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 
    has_many :exam_registrations, through: :lessons 

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

答えて

5

は同じ問題と格闘誰のために

を解決し、この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', :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 

アプリ/資産/ 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(/([ #;&,.+*~\':"!^$[\]()=>|\/@])/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() 

は、サーバやメニューの作業を再開します!

+1

この小さな変更の後、私はこの情報に感謝しました。 'config.register_javascript 'exam_registrations'' – blotto

+0

うれしい私は助けることができました! –

+0

@ Ryan.lay:http://stackoverflow.com/questions/28187354/active-admin-populate-one-of-the-second-drop-down-after-firstをご覧ください。 – inquisitive

関連する問題