virtus icon indicating copy to clipboard operation
virtus copied to clipboard

Hash.try_convert(attributes) errors on params in Rails 5

Open natesholland opened this issue 7 years ago • 0 comments

I'm running into an error specifically with this line of code: https://github.com/solnic/virtus/blob/4db9ffd380b5e42baa56b9f96b8daebf12fad48f/lib/virtus/attribute_set.rb#L196

In Rails 4 and below calling to_hash on parameters was easy and returned the expected hash as follows:

> ::Hash.try_convert(ActionController::Parameters.new({foo: "bar"}))
 => {"foo"=>"bar"}

However this behavior changed in Rails 5 and started to raise an error:

> ::Hash.try_convert(ActionController::Parameters.new({foo: "bar"}))
 => ActionController::UnfilteredParameters (unable to convert unpermitted parameters to hash)

Therefore I think Virtus needs to be slightly more intelligent about how it coerces to a hash. I have gone ahead and implemented a PR that should fix the issue I am seeing as well as a test to replicate the issue and verify it's fix in #388

natesholland avatar Apr 12 '18 19:04 natesholland