Display custom label for User in rails_admin paper

2019-09-17 13:45发布

问题:

When I go to a paper_trail history page in rails_admin, I see User objects in the User column (#<User:0x007f59a9...). Is there a way to configure this to use one of the User attributes? I thought I could use object_label_method but I don't seem to be having any luck.

Update:

I thought this was purely a display issue but paper_trail is actually storing Strings like #<User:0x007f59a9... in the whodunnit column of the versions table rather than IDs. I have overridden user_for_paper_trail in ApplicationController and while this successfully logs versions with User IDs in my frontend app, it does not in rails_admin. According to this thread, rails_admin should see my custom method because "RailsAdmin::ApplicationController inherits from your ::ApplicationController". Any thoughts on why this is happening and how I can get paper_trail to store User IDs?

回答1:

More digging revealed that this is a known problem with some combinations of versions of the gems. Here is a recent rails_admin pull request that addresses it. Here is a more complete description of the issue.

For now, I'm monkey patching in the fix by putting the following code in config/initializers/rails_admin_user_for_paper_trail.rb:

RailsAdmin::ApplicationController.module_eval do
  def user_for_paper_trail
    _current_user.try(:id) || _current_user
  end
end

For reference, I'm using paper_trail 3.0.6, rails_admin 0.6.6 and Rails 4.2.0.