Rails avoid writing attribute of nested object to log

孤人 提交于 2019-12-13 02:15:06

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/2308891/rails-avoid-writing-attribute-of-nested-object-to-log

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!