2013-07-03 28 views
8

Tôi muốn thực hiện một lựa chọn tùy chọn động thông qua formtastic Activeadmin 's như vậy:Activeadmin động formtastic chọn

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 

đang nghiệp dư của tôi là rõ ràng không phải làm việc như tôi dự định nó. Tôi nên thay đổi những gì?

Tôi có 4 mô hình như sau:

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 

Trả lời

5

SOLVED

Đối với bất cứ ai khác vật lộn với cùng một vấn đề, xem xét điều này railscast

dưới đây là cách tôi thực hiện nhiều chọn menu động trong 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 

ứng dụng/tài sản/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() 

khởi động lại máy chủ và các menu làm việc !

+1

cảm ơn thông tin, điều này làm việc cho tôi sau sự thay đổi nhỏ này. 'config.register_javascript 'exam_registrations'' – blotto

+0

Vui vì tôi có thể giúp! –

+0

@ Ryan.lay: bạn có thể vui lòng xem http://stackoverflow.com/questions/28187354/active-admin-populate-one-of-the-second-drop-down-after-first – inquisitive

Các vấn đề liên quan