Last Updated: January 06, 2019
·
4.513K
· andrewhao

Stopping rails from duplicating JSON parameters in a root node

If you're noticing that your controllers are receiving duplicate parameters under a root node when sending JSON (perhaps from a Backbone model):

{:id => 1,
 :title => "Moby Dick",
 :book => {
   :id => 1,
   :title => "Moby Dick"
}}

Then you likely initialized your Rails app with wrap_parameters on (for JSON) by default.

Grab your config/initializers/wrap_parameters.rb file and remove :json from the wrap_parameters format list:

ActiveSupport.on_load(:action_controller) do
  wrap_parameters :format => [] if respond_to?(:wrap_parameters)
end

2 Responses
Add your response

Wouldn't be better to remove it at all? Why leaving empty array here?

over 1 year ago ·

Thanks Drew. So annoying when your json also contains readAsDataURL result value.

over 1 year ago ·