The ActiveAdmin gem for Ruby on Rails is great to use if you don’t plan on customizing it too much. As a good friend of mine said, it gives you 99% for free, but to get it to do that 1% that you need ends up costing you more than the 99% you got for free in the first place. I wanted to simply customize the resource before it got saved – not that simple, it seems.
I’ve given up with ActiveAdmin on previous project I did and ended up writing my own admin interface and using Bootstrap for the interface. I’m currently in the early stages of another project which may not need to level of customizing as the previous project, so I hope we won’t need any functionality that would require severe hacking of ActiveAdmin at some point.
Anyway, I’m using the awesome Easy Roles gem by Platform45 in the project and I wanted the users created in ActiveAdmin to automatically have the “admin” role added to their roles. The ActiveAdmin documentation again let me down and after some heavy googling and going through the source I figured it out. This is how I did it:
# app/admin/users.rb
ActiveAdmin.register User do
controller do
def create
create! do |format|
format.html {
# add default role of admin for now
resource.add_role 'admin'
resource.save
}
redirect_to '/admin/users'
end
end
end
end
Leave a comment if this saved you a few gray hairs.
