awesome-print / awesome_print

Pretty print your Ruby objects with style -- in full color and with proper indentation
http://github.com/michaeldv/awesome_print
MIT License
4.07k stars 454 forks source link

ActionController::Parameters support #289

Open glaucocustodio opened 7 years ago

glaucocustodio commented 7 years ago

As of Rails 5 (https://github.com/rails/rails/pull/20868) calling params in a controller does not return a hash but a ActionController::Parameters.

If we do ap params on Rails 5, we don't leverage this gem, we gotta do ap params.to_h.

Should we implement calling to_h in any instance of ActionController::Parameters?

Thank you.

duffyjp commented 7 years ago

Calling .to_h we lose class name display and the permitted: true/false for each ActionController::Parameters object in the hash.

params.inspect

"licensings_attributes"=><ActionController::Parameters {"0"=><ActionController::Parameters {"license_number"=>"", "license_id"=>"1", "expiration_date"=>""} permitted: true>}

ap params.to_h

  "licensings_attributes" => {
        "0" => {
             "license_number" => "",
                 "license_id" => "1",
            "expiration_date" => ""
        }

ap params

[Exception] DEPRECATION WARNING: #to_hash unexpectedly ignores parameter filtering, and will change to enforce it in Rails 5.1. Enable raise_on_unfiltered_parameters to respect parameter filtering, which is the default in new applications. For the existing deprecated behaviour, call #to_unsafe_h instead.

sebbean commented 6 years ago

what's the move here? breaking my rails 5.1 project

imajes commented 6 years ago

@sebbean you're very welcome to submit a patch! :)

sebbean commented 6 years ago

@imajes actually turns out to be working just fine! I just wasn't sending Content-Type: app/json so it wasn't parsing the params correctly. Was assuming form-data, thus params where essentially null.

Still seems to be a weird error for that case tho.

janwerkhoven commented 2 years ago

To circumvent:

*** ActionController::UnfilteredParameters Exception: unable to convert unpermitted parameters to hash

... you can use this hack: ap params.to_enum.to_h.

+1 for ap params

philCryoport commented 1 year ago

@janwerkhoven wrote:

... you can use this hack: ap params.to_enum.to_h

That works if you want it the display to be on STDOUT -- alas, if I instead want that pretty-print to be recorded to the debug log, and I try Rails.logger.debug ("#{params.to_enum.to_h.ai}"), it throws an error:

ActionController::UnfilteredParameters - unable to convert unpermitted parameters to hash:
  app/controllers/reports/exports_controller.rb:15:in `create'
  app/controllers/application_controller.rb:334:in `block in wrap_in_user_time_zone'
  app/controllers/application_controller.rb:334:in `wrap_in_user_time_zone'
  app/middleware/healthcheck_middleware.rb:21:in `call'
duffyjp commented 1 year ago

@philCryoport You can probably work around that with a .permit!

philCryoport commented 1 year ago

@philCryoport You can probably work around that with a .permit!

Thank you! That worked!