Easier links in ActiveAdmin using auto_link

I both love and hate building admin interfaces with ActiveAdmin. Let’s talk about a thing I like about.

When building an admin interface, it’s inevitable that you’ll need to link to related objects in the system. For example, on an order page, it’s great if you can link to the user who made the order and vice versa. These types of links are really useful, so you will likely be coding quite a few of them.

But coding them can be cumbersome. I always have to grep the routes to figure out the route needed to link to another resource. And calls to link_to often end up being super long and ugly.

It turns out, ActiveAdmin contains a method that completely solves this problem - auto_link. With auto_link you won’t have to look up any routes and all your long link_to calls will become much shorter. Let’s look at an example.

This is what I used to write:

link_to order.user.name, admin_user_path(order.user)  

Now, I write this:

auto_link order.user  

That second example generates the exact same link - awesome!

Link Text

Conveniently, auto_link will often generate useful text for the link using a series of fallbacks. When the default text is not what you want, you can simply provide a second argument to the method like this:

auto_link(user, 'my fancy text')  

I found auto_link by browsing the source for the ActiveAdmin demo site; it’s a very helpful supplement to the ActiveAdmin documentation.