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
Written by Andrew Hao
Related protips
2 Responses

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
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#