Politics, Programming and Possibilities
23 May
I recently discovered that xml data is automatically parsed by the latest Rails (probably for quite some time) and added to the params hash. What’s more, it turns on that adding Json data to params isn’t too bad, either. Here’s the code I added to my environment.rb file:
ActionController::Base.param_parsers[Mime::JSON] = Proc.new do |post|
RAILS_DEFAULT_LOGGER.debug "JSON parser: #{post.inspect}"
# Just to be gracious, we'll ignore javascript 'undefined' and 'null' strings
if post and !["", "undefined", "null"].include?(post)
decoded = ActiveSupport::JSON::decode(post)
if !decoded.respond_to?(:update)
RAILS_DEFAULT_LOGGER.warn "WARNING: "
"JSON parameters not in hash form (was an array sent?)"
end
decoded
else
{}.with_indifferent_access
end
end
(Wow, two rails posts in a row!)
Leave a reply