Skip to content

Instantly share code, notes, and snippets.

@xaethos
Last active December 20, 2015 16:39
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 xaethos/6163374 to your computer and use it in GitHub Desktop.
Save xaethos/6163374 to your computer and use it in GitHub Desktop.
Convert as_json hash to controller params, for testing ease
def to_form_param json
case json
when Array
return json if json.empty? || !json[0].is_a?(Hash)
i = 0
json.reduce({}.with_indifferent_access) do |memo, item|
memo["#{i}"] = to_form_param item
i += 1
memo
end
when Hash
out = json.dup.with_indifferent_access
out.each do |key, value|
out[key] = to_form_param value
end
out
else
json
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment