Skip to content

Instantly share code, notes, and snippets.

@barockok
Created August 11, 2014 02:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barockok/477c6a2be47fd1d5a8aa to your computer and use it in GitHub Desktop.
Save barockok/477c6a2be47fd1d5a8aa to your computer and use it in GitHub Desktop.
Repair Rails nested params
class ApplicationController < ActionController::Base
before_action :repair_nested_params
# -- your code here
protected
def repair_nested_params(obj = params)
obj.each do |key, value|
if value.is_a? Hash
# If any non-integer keys
if value.keys.find {|k, _| k =~ /\D/ }
repair_nested_params(value)
else
obj[key] = value.values
value.values.each {|h| repair_nested_params(h) }
end
end
end
end
end
@bkazez
Copy link

bkazez commented Jan 9, 2021

For Rails 5, I replaced line 9 with:

if value.respond_to?('keys')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment