StrongParameters .rename()

Hi,

Assume I’m doing an API in Rails 4 with StrongParameters, and I got a model like Pricing, with many Prices.

I want my API to support in one endpoint nested attributes for the Prices of a Pricing.

The consumer of the API doesn’t care that I am using Rails, so he sends me the nested attributes of the Prices as :prices, but Rails is expecting :prices_attributes

And I am just wondering if it could be possible to bring extra options on Strong Parameters, to rename keys like:

params.permit(:prices, :description, ...)
params.rename(:prices, :prices_attributes)

I don't think adding to strong params make sense, but you can just mutate the hash you get back from #permit as you need to after the call. Maybe using #tap.

Hi.

Not sure if this helps, but I wrote a gem to deal with this exact use case: to define a clear mapping between incoming payloads (that I might have no control of) and the internal data structures that need to feed to my models.

https://github.com/ismasan/hash_mapper

It only really make sense if you have lots of mismatching fields or the nested structures are different. For simpler cases just mutating what you need is probably enough.

You might find it useful anyway.

I do so:
params

.tap { |whitelisted|

whitelisted[:prices_attributes] = params[:prices] if params.has_key? :prices

}

.permit(

:description, prices_attributes: [:attr_1, :attr_22], …

)