Search code examples
ruby-on-railsrubyruby-on-rails-5.2

Monkey patch unpermitted_parameters in actioncontroller


I was going through one of the PR in rails 6 where they have added a color property to the unpermitted parameter. I found it quite imperative for my application because we usually have a very long log. I thought to include the same functionality in my Rails 5 application

I created an ext folder inside my app and there added the below code

And in initializers folder

require "#{Rails.root}/app/ext/action_controller_override.rb"

file_name: action_controller_override.rb

ActionController::LogSubscriber.class_eval do

  def unpermitted_parameters(event)
      debug do
        unpermitted_keys = event.payload[:keys]
        color("Unpermitted parameter#{'s' if unpermitted_keys.size > 1}: #{unpermitted_keys.map { |e| ":#{e}" }.join(", ")}", RED)
      end
    end

end

But the above code giving me error.

Could not log unpermitted_parameters.action_controller event. NameError: uninitialized constant RED

So, can anyone shed a light on it and tell me where I am going wrong


Solution

  • Finally, I found the solution

    "Unpermitted parameter#{'s' if unpermitted_keys.size > 1}: #{unpermitted_keys.map { |e| ":#{e}" }.join(", ")}".green
    

    The keyword green is given by colorize gem