简体   繁体   English

使用Paper Trail为Rails Admin定制Whodunnit信息

[英]Customising Whodunnit information for Rails Admin using Paper Trail

I have been able to correctly append the name of a user into the Whodunnit field for actions by adding this to my application_controller 通过将用户名添加到我的application_controller ,我已经能够正确地将用户名追加到Whodunnit字段中以进行操作

def user_for_paper_trail                                                                                     
    current_user ? current_user.name : 'Public user'                                                           
end

This works fine. 这很好。

If a user makes a change using Rails Admin however, the whodunnit is still set to the user id. 但是,如果用户使用Rails Admin进行更改,whodunnit仍设置为用户ID。

Is there a method I can call that would tell paper trail what detail I want to store when a record is being updated via Rails Admin? 当通过Rails Admin更新记录时,是否可以调用一种方法来告诉Paper Trail我想存储的详细信息?

RailsAdmin has had some issues with this. RailsAdmin对此存在一些问题。 You will find the following helpful: Display custom label for User in rails_admin paper_trail history . 您会发现以下帮助: 在rails_admin paper_trail历史记录中显示User的自定义标签

Also, because the field is supposed to store IDs (even though it's a string field), I'd keep the IDs and store something nonsensical for the 'Public user' such as '0' (referenced by a constant, of course). 另外,由于该字段应该存储ID(即使它是一个字符串字段),所以我会保留ID并存储一些对'Public user'无意义的内容,例如'0' (当然,用常量引用)。 I would then override the whodunnit method to return 'Public user' in case super == '0' . 然后,在super == '0'情况下,我将覆盖whodunnit方法以返回'Public user' super == '0'

That approach would be particularly helpful if you already have a bunch of versions with IDs in their whodunnit fields or if you have multiple users with the same name. 如果您已经有很多版本的whodunnit字段中有ID,或者您有多个具有相同名称的用户,则该方法将特别有用。

PS To quickly confirm that RailsAdmin is completely ignoring / not ignoring the controller method you've defined, just force-raise an error in the method. PS要快速确认RailsAdmin完全忽略/不忽略您已定义的控制器方法,只需强行引发方法中的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM