ActiveAdmin has saved a huge amount of time on our current project, and I highly recommend it for quickly giving non-technical people administrator access to your Rails app.
Some of the documentation is great, and there are lots of methods you can use to customize, but there are also some out-of-date red herrings, some things that require learning about Formtastic, etc. So here’s a minimalist guide to customizing ActiveAdmin forms relying more on Ruby and logic than on the unique ins and outs of ActiveAdmin.
Tip One
Use the ActiveAdmin documentation that’s on Github . The top hit on Google is out of date!
Tip Two
Selects and check boxes can take any arrays of tuples. The first item in the tuple is what will be displayed, and the second item is the value: [[“Display Name One”, 1], [Display Name Two”, 2]]
. In practice, you’ll probably grab some array from ActiveRecord and do map on it like so:
input :toppings, :as => :check_boxes, :collection => Toppings.all.map { |t| [t.name, t.id] }
Tip Three
Create a module for ActiveAdmin helper functions. It’ll have access to ActiveRecord for your app and you can write code like you would anywhere else. I found it made my code more readable and saved me a lot of time and frustration because I could just write regular Rails code. For example:
/*In AdminHelpers module:*/
def self.sorted_available_instructors
booked_instructor_ids = Classes.pluck(:instructor_id)
available_instructor_ids = Instructor.pluck(:id) - booked_instructor_ids
available_instructors = available_instructor_ids.map{|instructor_id|
instructor = Instructor.find(instructor_id)
[instructor.name, instructor.id]
}
available_instructors.sort {|a, b| a[0] <=> b[0]}
end
In the form for editing a class, you can then create a specialized drop-down with:
input :instructor_id, as: :select, collection: AdminHelpers::sorted_available_instructors
Note that there are ActiveAdmin methods for sorting, filtering, etc., and sometimes you can get away with a one-line declaration. I just found that, often, writing my own method was faster and made my code more readable.
Tip Four
It’s handy to condition on f.object.new_record?
to know whether the record is new or not. That way, if there’s just a small difference between creating a resource and editing a resource, you can use the same form code for both.
if f.object.new_record?
input :instructor, as: :select, collection: ActiveAdminHelpers.sorted_available_instructors
else
input :instructor, as: :select, collection: ActiveAdminHelpers.instructor_choices_for_class(f.object.instructor_id),
end
You can mix and match these tips to customize your ActiveAdmin forms using your Ruby skills. But if you want to get into the weeds, you can also poke around the ActiveAdmin documentation (on GitHub!) for just the method you need.
Great write-up! Definitely some useful tips you got there.
Even though it is quite powerful, I’ve seen firsthand how ugly it can get on bigger implementations. That’s actually why we’ve started an admin interface of our own: Forest – forestadmin.com . As an ActiveAdmin power user, I’d love to have your feedback on our product! :)