Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3.1 - Active_admin and Checkboxes

This question is really two questions.

  1. Select all button - Active_admin uses formtastic to render forms, so I'm going to ask in the context of formtastic. How would I create a button that selects all the checkboxes on the page? I could do it using JavaScript, but I'm unsure of how to do this in formtastic.
  2. Set collection based on select value. I have a drop down menu that let's people choose from a list of "Courses". Students are enrolled in courses, so I want to be able to display a checkbox list of student enrolled on the course. Ie: The list of students will change if the user selects a different course.

    course.rb

    has_and_belongs_to_many :students

    student.rb

    has_and_belongs_to_many :courses

like image 305
Ammar Avatar asked Dec 27 '22 13:12

Ammar


1 Answers

Formtastic does not have an easy solution for a "select all" checkbox, if you are using JQUERY you can do this

In your student.rb model add

attr_accessor :select_all_courses

f.inputs "Courses" do
  f.input :select_all_courses, :as => :boolean, :label => 'SELECT ALL', :input_html => {:onclick => "jQuery.each( $('.student_courses_checkboxes'), function() { this.checked = $('.all_selector')[0].checked });", :class => "all_selector"}  
  f.input :courses, :as => :check_boxes, :collection => @courses, :input_html => {:class => 'student_courses_checkboxes'}      
end

The best you can do in active admin with formtastic is:

In your course form

f.input :students, :as => :check_boxes, :collection => @students   
like image 71
vhtellez Avatar answered Jan 14 '23 03:01

vhtellez