Created
August 11, 2014 02:28
-
-
Save barockok/477c6a2be47fd1d5a8aa to your computer and use it in GitHub Desktop.
Repair Rails nested params
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For Rails 5, I replaced line 9 with:
if value.respond_to?('keys')