6

I am using Active Admin with Ruby on Rails and I am having an issue with the way that some models are shown in the panel.

Taking the class User as an example, if I do not define any method to display it friendly, I see #<User:00000006b47868>. So Active Admin suggests implementing a method to specify, for each class, how to show it.

According to the documentation (http://activeadmin.info/docs/3-index-pages/index-as-table.html), Active Admin looks for one of these methods to guess what to display, in the following order:

:display_name, :full_name, :name, :username, :login, :title, :email, :to_s

So having this method within the User class would solve the problem:

def display_name
  return self.id.to_s + '-' + self.full_name
end

However, before using Active Admin, I was already using the method display_name with other purposes (for example, in views) in order to show the user name in a friendly way, and I do not want to show the same in Active Admin panel.

I cannot change the name of the method because I use display_name in a lot of files along the project, and changing it would probably introduce bugs in the application.

An ideal solution for this case would be to have something like an active_admin_name method that is used by Active Admin to show models in its panel. So the question is:

Is there any way to have a method that is called by Active Admin instead of display_name? For example, to result in the following order:

:active_admin_name, :display_name, :full_name, :name, :username, :login, :title, :email, :to_s

I have searched in Active Admin documentation and in config/initializers/active_admin.rb, but I could not find a way to do it.

Thanks!

2 Answers 2

12

Try

ActiveAdmin.setup do |config|    
  config.display_name_methods = [:active_admin_name, :display_name ...]
end

You can find this setting in lib/active_admin/application.rb

Sign up to request clarification or add additional context in comments.

3 Comments

I think I do not have that file in my project. I tried creating it and putting that code there, but it did not work. I also tried putting that code in config/initializers/active_admin.rb, but no good results neither. I have found that in Active Admin source code there is some code like yours: github.com/gregbell/active_admin/blob/master/lib/active_admin/… Maybe we have to put it within other file?
It works for my project which uses AA 0.6.0. I defined config.display_name_methods = [:admin_display_name, ....] in config/initializers/active_admin.rb. Be sure you restart your app.
@eze.scaruli Should be marked as correct answer. Note that the configuration can be in different places, for me it is in config/initializers/active_admin.rb.
0

You could also use :title key to define it for each page, like this:

show(title: 'Something') do |record|
  ...
end

# or with a Proc
show(title: ->(record) { record.another_method_to_display }) do |record|
  ...
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.