Skip to content

Instantly share code, notes, and snippets.

@cch1
Created December 29, 2009 16:02
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 cch1/265388 to your computer and use it in GitHub Desktop.
Save cch1/265388 to your computer and use it in GitHub Desktop.
# Recursively coerce empty string values in hash -particularly
# useful for HTTP POSTed forms.
# Values are coerced based on the rules mapping of params_hash keys to coerced values.
# See http://dev.rubyonrails.org/ticket/5694 for background
def self.coerce_empty_strings(params_hash, rules = {})
return unless params_hash
params_hash.each_pair do |k,v|
# AR::Base.extract_callstack_for_multiparameter_attributes can't handle nil values
unless k.include?('(')
params_hash[k] = rules[k.to_sym] if v.kind_of?(String) && v.empty?
params_hash[k] = coerce_empty_strings(v) if v.kind_of?(Hash)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment