Rails避免将嵌套对象的属性写入日志 [英] Rails avoid writing attribute of nested object to log

查看:68
本文介绍了Rails避免将嵌套对象的属性写入日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何防止Rails中的嵌套关​​系的某个参数进入日志文件-我正在将大型文件写入db中的列,并且不希望Rails将其写入日志文件中..我知道 filter_parameter_logging 的功能,但似乎不适用于嵌套模型-我可能只是把位置错误了?

How can I prevent a certain parameter of a nested relationship in rails from entering the log file - I am writing LARGE files to a column in the db and don't want rails to write that to the log file.. I know of filter_parameter_logging but it doesn't seem to work for nested models - I may just be putting in the wrong spot?

推荐答案

根据Rails代码,即使对于嵌套的参数哈希,它也应能正常工作. 您可以在控制器上实现filter_parameters方法来解决您的问题.阅读主题以了解更多详细信息. 为了方便起见,我从上面的线程中发布了代码.

According to the Rails code this should work even for nested parameter hashes. You can implement the filter_parameters method on your controller to work around your issue. Read this thread for more details. I have posted the code from the thread above for your convenience.

  def filter_parameters(unfiltered)
    return unfiltered unless params[:action]  == 'payment'
    filtered = unfiltered.dup
    filtered[:creditcard] = unfiltered[:creditcard].dup
    filtered[:creditcard][:number] = '[FILTERED]'
    filtered[:creditcard][:type] = '[FILTERED]'
    filtered
  end

这篇关于Rails避免将嵌套对象的属性写入日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆