Skip to content

Instantly share code, notes, and snippets.

@taavo
Forked from Unpakt/rails 3.2.11 params patch
Last active December 11, 2015 00:48
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 taavo/4518541 to your computer and use it in GitHub Desktop.
Save taavo/4518541 to your computer and use it in GitHub Desktop.
Add array support
def repair_empty_param_associations
params.keys.each do |key|
repair_nested_params(params, key, params[key])
end
end
private
def repair_nested_params(current_params, key, value)
if key =~ /^(.*)_attributes$/ && value.nil?
current_params[key] = []
elsif value.is_a? Array
value.each_with_index do |nested_value, i|
repair_nested_params(value, i, nested_value)
end
elsif value.is_a? Hash
value.keys.each do |nested_key|
repair_nested_params(value, nested_key, value[nested_key])
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment